Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas converting floats to strings without decimals

Tags:

python

pandas

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 
like image 868
Brian Avatar asked Jul 22 '16 00:07

Brian


People also ask

How do I convert a float to a string in Python?

We can convert float to a string easily using str() function.


2 Answers

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 
like image 157
maxymoo Avatar answered Sep 19 '22 17:09

maxymoo


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 
like image 30
piRSquared Avatar answered Sep 22 '22 17:09

piRSquared