简体   繁体   中英

Python TypeError: cannot convert the series to <class 'int'> when trying to do math on dataframe

I have a data frame that looks something like this:

defaultdict(<class 'list'>, {'XYF':             TimeUS           GyrX           GyrY           GyrZ         AccX  \
0        207146570    0.000832914    0.001351716  -0.0004189798    -0.651183   
1        207186671    0.001962787    0.001242457  -0.0001859666   -0.6423497   
2        207226791   9.520243E-05    0.001076498  -0.0005664826   -0.6360412   
3        207246474   0.0001093059    0.001616917   0.0003615251   -0.6342875   
4        207286244    0.001412051   0.0007565815  -0.0003780428    -0.637755   


[103556 rows x 12 columns], 'DAR':           TimeUS RSSI RemRSSI TxBuf Noise RemNoise RxErrors Fixed
0      208046965  159     161    79    25       29        0     0
1      208047074  159     161    79    25       29        0     0
2      208927455  159     159    91    28       28        0     0
3      208927557  159     159    91    28       28        0     0


[4136 rows x 8 columns], 'NK2':            TimeUS    IVN    IVE   IVD    IPN   IPE    IPD IMX  IMY IMZ  IYAW  \
0       207147350  -0.02   0.02  0.00  -0.02  0.01   0.20   0    0   0  1.94   
1       207187259  -0.02   0.02  0.00  -0.02  0.01   0.20   0    0   0  1.94   
2       207227559  -0.02   0.02  0.00  -0.02  0.01   0.14   0    0   0  1.77   
3       207308304   0.02   0.02  0.00  -0.01  0.01  -0.05   0    0   0  1.77   
4       207347766   0.02   0.02  0.00  -0.01  0.01  -0.05   0    0   0  0.82  

I first separated the column I want to do math with:

new_time = dfs['XYF']['TimeUS']

Then I have tried several things to do some math on it but I had no luck. First I just treated it like a list. so

new_time_F = new_time / 1000000

That didn't work, gave me a float error of:

TypeError: unsupported operand type(s) for /: 'str' and 'int'

so I did this:

new_time_F = float (new_time) / 1000000

This give me an error:

TypeError: cannot convert the series to <class 'float'>

I have no idea where to go from here.

What if you do this (as was suggested earlier):

new_time = dfs['XYF']['TimeUS'].astype(float)
new_time_F = new_time / 1000000

Seems your initial data contains strings and not numbers. It would probably be best to ensure that the data is already of the required type up front.

However, you can convert strings to numbers like this:

pd.Series(['123', '42']).astype(float)

instead of float(series)

您可以从pd.to_numeric(s)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM