Python TypeError:尝试对数据帧进行数学运算时,无法将系列转换为 <class 'int'>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41000428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:20:45  来源:igfitidea点击:

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

pythonpandas

提问by Jassica Mao

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.

我不知道从这里去哪里。

回答by AlexG

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

如果你这样做(正如之前建议的那样):

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

回答by languitar

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)

代替 float(series)

回答by kashanipour

You can use from the pd.to_numeric(s)

您可以从 pd.to_numeric(s)