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
We can convert float to a string easily using str() function.
Converting to int
(i.e. with .astype(int).astype(str)
) won't work if your column contains nulls; it's often a better idea to use string formatting to explicitly specify the format of your string column; (you can set this in pd.options
):
>>> pd.options.display.float_format = '{:,.0f}'.format >>> df.astype(float).sum() 0 7 1 4 2 11 dtype: float64
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With