chowpay
chowpay

Reputation: 1687

Pandas dataframe stripping content

I have data that looks like this

    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

I want to make a column of totals for MBs and GBs to do that I first need to strip off the MB and GB.

I tried this

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'))

But I keep getting this error: AttributeError: 'numpy.float64' object has no attribute 'rstrip'

Searched around slack people seem to have gotten this to work. What am I doing wrong?

Upvotes: 0

Views: 97

Answers (1)

BENY
BENY

Reputation: 323386

You can using 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

Or maybe using 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

Upvotes: 1

Related Questions