熊猫数据框剥离内容

时间:2018-08-15 00:57:26

标签: pandas dataframe jupyter-notebook

我有类似这样的数据

    Date            MBs     GBs
0   2018-08-14 20:10    32.00 MB    0.00 GB
1   2018-08-14 20:05    4.00 MB     0.00 GB
2   2018-08-14 20:00    1000.99 MB  1.23 GB

我要在MB和GB的总计列中做一件事,我首先需要剥离MB和GB。

我尝试过

df['Data Transferred (MB)'] = df['Data Transferred (MB)'].map(lambda x: x.rstrip('MB'))
df['Data Transferred (GB)'] = df['Data Transferred (GB)'].map(lambda x: x.rstrip('GB'))

但我不断收到此错误: AttributeError:“ numpy.float64”对象没有属性“ rstrip”

在闲散的地方搜索的人似乎已经开始使用它了。我在做什么错了?

1 个答案:

答案 0 :(得分:1)

您可以使用pandas str.strip

df['MBs']=df['MBs'].str.strip('MB')
df['GBs']=df['GBs'].str.strip('GB')
df
Out[1067]: 
                 Date       MBs    GBs
0 2018-08-14 20:10:00    32.00   0.00 
1 2018-08-14 20:05:00     4.00   0.00 
2 2018-08-14 20:00:00  1000.99   1.23

或者使用replace

df.replace({' MB':'',' GB':''},regex=True)
Out[1070]: 
               Date      MBs   GBs
0  2018-08-14 20:10    32.00  0.00
1  2018-08-14 20:05     4.00  0.00
2  2018-08-14 20:00  1000.99  1.23