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

Jas*_*Mao 9 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  
Run Code Online (Sandbox Code Playgroud)

我首先将我想要数学的专栏分开:

new_time = dfs['XYF']['TimeUS']
Run Code Online (Sandbox Code Playgroud)

然后我尝试了几件事来做一些数学计算,但我没有运气.首先,我把它当作一个列表来对待它.所以

new_time_F = new_time / 1000000
Run Code Online (Sandbox Code Playgroud)

这没用,给了我一个浮动错误:

TypeError: unsupported operand type(s) for /: 'str' and 'int'
Run Code Online (Sandbox Code Playgroud)

所以我这样做了:

new_time_F = float (new_time) / 1000000
Run Code Online (Sandbox Code Playgroud)

这给了我一个错误:

TypeError: cannot convert the series to <class 'float'>
Run Code Online (Sandbox Code Playgroud)

我不知道从哪里开始.

Ale*_*exG 18

如果你这样做(如前所述):

new_time = dfs['XYF']['TimeUS'].astype(float)
new_time_F = new_time / 1000000
Run Code Online (Sandbox Code Playgroud)

  • 事实上,您不能将“astype”应用于数据帧。请改用“applymap”。例如 `dfs['XYF'] = dfs['XYF'].applymap(float)` (2认同)