I am trying to change a string to a float value in a dataframe.
#showing dataframe for illustration`
IN: df2[:5]
OUT: TRD_EXCTN_DT ASCII_RPTD_VOL_TX
0 08/13/2010 1000000
1 08/16/2010 1MM+
2 08/16/2010 369000
3 08/16/2010 1MM+
4 08/16/2010 1MM+
output concatenates the string even though I thought I had changed it to a float. I need to operate on the number so a string does not work.
IN: df2.loc[df2['ASCII_RPTD_VOL_TX'] == '1MM+', 'ASCII_RPTD_VOL_TX'] = 1000005
df2.ASCII_RPTD_VOL_TX.astype(float)
df2['b'] = df2['ASCII_RPTD_VOL_TX'] + df2['ASCII_RPTD_VOL_TX']
df2[:3]
OUT: TRD_EXCTN_DT ASCII_RPTD_VOL_TX b
0 08/13/2010 1000000 10000001000000
1 08/16/2010 1000005 2000010
2 08/16/2010 369000 369000369000`
also,
In[22]:df2.dtypes
Out[22]:TRD_EXCTN_DT object
ASCII_RPTD_VOL_TX object
b object
dtype: object
You want to change your second line to df2['ASCII_RPTD_VOL_TX']=df2.ASCII_RPTD_VOL_TX.astype(float)
.
The reason you are getting strange result is that your 1st and 3rd row, ASCII_RPTD_VOL_TX column cells are still in str
. +
is doing string concatenate
there for those cells.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.