Python 类型错误 : cannot convert the series to <class 'int' > when trying to do math on dataframe

标签 python pandas

我有一个看起来像这样的数据框:

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  

我首先分隔了我想做数学运算的列:

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

然后我尝试了几种方法来对其进行一些数学计算,但我没有运气。 首先,我只是把它当作一个列表。所以

new_time_F = new_time / 1000000

那没有用,给我一个 float 错误:

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

所以我这样做了:

new_time_F = float (new_time) / 1000000

这给我一个错误:

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

我不知道从这里去哪里。

最佳答案

如果您这样做(如前所述)会怎样:

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

关于Python 类型错误 : cannot convert the series to <class 'int' > when trying to do math on dataframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41000428/

相关文章:

python - 用另一个数据帧迭代更新数据帧的值

python - 在 for 循环中连接字典

python - 对字典中的值执行循环移位

python - 使用 seaborn distplot 设置轴最大值

python - 如何比较 Pandas 数据框列中可用的十进制数?

python - 将两个 Pandas 数据帧与仅添加整数计数相结合

python - 在 python 中将聚合值拆分为不同的计数,返回新行

python - Pandas 按字符串过滤

python - 为 "assertEqual"和 "assertNotEqual": should I bother? 编写测试

Python 选择特定的行和列