如何在python中的列中选择一个n值

时间:2017-04-24 09:07:35

标签: python pandas dataframe

我的熊猫数据框看起来像这样:

timestamp            S
2017-04-17 00:00:05  4300
2017-04-17 00:00:10  4297
2017-04-17 00:00:15  4321
2017-04-17 00:00:25  4335
...
2017-04-17 23:59:55  4287

如果df['S']的值可以被视为abcd,我想对每一行进行以下计算:

df['x'] = (df['S'][bcd]/1000)*(10**df['S'][a]) 

所以我得到了:

timestamp            S    x
2017-04-17 00:00:05  4300 3000
2017-04-17 00:00:10  4297 2970
2017-04-17 00:00:15  4321 3210
2017-04-17 00:00:25  4335 3350
...
2017-04-17 23:59:55  4287 2870

我该怎么做?

1 个答案:

答案 0 :(得分:2)

似乎你需要:

df['x'] = df['S'].astype(str)
df['x'] = (df['x'].str[-3:].astype(int) /1000 * (10**df['x'].str[0].astype(int))).astype(int)
print (df)
                        S     x
timestamp                      
2017-04-17 00:00:05  4300  3000
2017-04-17 00:00:10  4297  2970
2017-04-17 00:00:15  4321  3210
2017-04-17 00:00:25  4335  3350
2017-04-17 23:59:55  4287  2870