Brian Brian - 2 years ago 184
Python Question

pandas converting floats to strings without decimals

I have a dataframe

df = pd.DataFrame([
['2', '3', 'nan'],
['0', '1', '4'],
['5', 'nan', '7']
])

print df

0 1 2
0 2 3 nan
1 0 1 4
2 5 nan 7


I want to convert these strings to numbers and sum the columns and convert back to strings.

Using
astype(float)
seems to get me to the number part. Then summing is easy with
sum()
. Then back to strings should be easy too with
astype(str)


df.astype(float).sum().astype(str)

0 7.0
1 4.0
2 11.0
dtype: object


That's almost what I wanted. I wanted the string version of integers. But floats have decimals. How do I get rid of them?

I want this

0 7
1 4
2 11
dtype: object

Answer Source

Add a astype(int) in the mix:

df.astype(float).sum().astype(int).astype(str)

0     7
1     4
2    11
dtype: object

Demonstration of example with empty cells. This was not a requirement from the OP but to satisfy the detractors

df = pd.DataFrame([
        ['2', '3', 'nan', None],
        [None, None, None, None],
        ['0', '1', '4', None],
        ['5', 'nan', '7', None]
    ])

df

      0     1     2     3
0     2     3   nan  None
1  None  None  None  None
2     0     1     4  None
3     5   nan     7  None

Then

df.astype(float).sum().astype(int).astype(str)

0     7
1     4
2    11
3     0
dtype: object

Because the OP didn't specify what they'd like to happen when a column was all missing, presenting zero is a reasonable option.

However, we could also drop those columns

df.dropna(1, 'all').astype(float).sum().astype(int).astype(str)

0     7
1     4
2    11
dtype: object
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download