Granted, the answer I linked in the comments is not very helpful. You can specify your own string converter like so.
In [25]: pd.set_option('display.float_format', lambda x: '%.3f' % x)
In [28]: Series(np.random.randn(3))*1000000000
Out[28]:
0 -757322420.605
1 -1436160588.997
2 -1235116117.064
dtype: float64
I'm not sure if that's the preferred way to do this, but it works.
Converting numbers to strings purely for aesthetic purposes seems like a bad idea, but if you have a good reason, this is one way:
In [6]: Series(np.random.randn(3)).apply(lambda x: '%.3f' % x)
Out[6]:
0 0.026
1 -0.482
2 -0.694
dtype: object
Answer from Dan Allan on Stack OverflowGranted, the answer I linked in the comments is not very helpful. You can specify your own string converter like so.
In [25]: pd.set_option('display.float_format', lambda x: '%.3f' % x)
In [28]: Series(np.random.randn(3))*1000000000
Out[28]:
0 -757322420.605
1 -1436160588.997
2 -1235116117.064
dtype: float64
I'm not sure if that's the preferred way to do this, but it works.
Converting numbers to strings purely for aesthetic purposes seems like a bad idea, but if you have a good reason, this is one way:
In [6]: Series(np.random.randn(3)).apply(lambda x: '%.3f' % x)
Out[6]:
0 0.026
1 -0.482
2 -0.694
dtype: object
Here is another way of doing it, similar to Dan Allan's answer but without the lambda function:
>>> pd.options.display.float_format = '{:.2f}'.format
>>> Series(np.random.randn(3))
0 0.41
1 0.99
2 0.10
or
>>> pd.set_option('display.float_format', '{:.2f}'.format)
I want to convert this number to an integer. I used a lot of ways but these ways couldn't convert it accurately.
Most of the ways convert this number to "67675656072510696" but "67675656072510693" is correct.
I wonder when I search this number by integer format, python can find that.
the number is "6.7675656072510696e+16".
num = 4.04e-10
Tried searching online but none of the methods actually worked:/
I am writing a simple program to help me do my semiconductors homework. I would like to use the input() function to have the user input a string in the format "1e15" for instance, meaning 1x10^15. How can I convert that string into an integer?
You can use map or apply, as mentioned in this comment:
print (df.userid.map(lambda x: '{:.0f}'.format(x)))
0 nan
1 109117800000
2 113785600000
Name: userid, dtype: object
df.userid = df.userid.map(lambda x: '{:.0f}'.format(x))
print (df)
userid
0 nan
1 109117800000
2 113785600000
I wondered whether map would be faster, but it is the same:
#[300000 rows x 1 columns]
df = pd.concat([df]*100000).reset_index(drop=True)
#print (df)
In [40]: %timeit (df.userid.map(lambda x: '{:.0f}'.format(x)))
1 loop, best of 3: 211 ms per loop
In [41]: %timeit (df.userid.apply(lambda x: '{:.0f}'.format(x)))
1 loop, best of 3: 210 ms per loop
Another solution is to_string, but it is slow:
print(df.userid.to_string(float_format='{:.0f}'.format))
0 nan
1 109117800000
2 113785600000
In [41]: (df.userid.to_string(float_format='{:.0f}'.format))
1 loop, best of 3: 2.52 s per loop
I just stumbled upon this problem after reading a dataframe from a json file using the read_json method and unfortunately it does not have a keep_default_na parameter.
The solution was to convert the long floats to np.int64 before converting them to str.
In [53]: tweet_id_sample = tweets.iloc[0]['id']
tweet_id_sample
Out[53]: 8.924206435553362e+17
In [54]: tweet_id_sample.astype(str)
Out[54]: '8.924206435553362e+17'
In [55]: tweet_id_sample.astype(np.int64).astype(str)
Out[55]: '892420643555336192'
In [56]: # This overflows
tweet_id_sample.astype(int)
Out[56]: -2147483648
I realised it was the infinity statement causing the issue in my data. Removing this with a find and replace worked.
@Anton Protopopov answer also works as did @DSM's comment regarding me not typing df['speed'] = df['speed'].astype(float).
Thanks for the help.
It's hard to say without seeing your data but it seems that problem in your rows that they contain something else except for numbers and 'n/a' values. You could load your dataframe and then convert it to numeric as show in answers for that question. If you have pandas version >= 0.17.0 then you could use following:
df1 = df.apply(pd.to_numeric, args=('coerce',))
Then you could drop row with NA values with dropna or fill them with zeros with fillna
Hi,
I am loading into Pandas number from excel sheet that has all formulas in it, and the number that is displayed as 39,213,555.73 pandas read as string such as 3.92136e+07. Column name is Values. This is on Windows.
Not sure how to remove scientific notation in entire data frame, all columns and rows. I tried pretty much everything and didn't work.