I have data in many columns like the following in pandas dataframe:
col1| col2| ...| col99 |col100
MBs| Gigabytes|...| MBs| |MBs
Megabytes| GBs|...|Megabytes |Gigabytes
GB | Megabytes| ...|Gigabytes|Gigabytes
GBs | GB |... |MBs |Gigabytes
Gigabytes|Megabytes|...|Gigabytes |Megabytes
I have also a dictionary which maps similar values. For example,
mapping = {'Megabytes':'MB', 'Gigabytes':'GB', 'MBs':'MB','GBs':'GB', 'GB':'GB',}
I want to replace each value in the column with mapped values in the dict. Currently I am trying to do something like this but getting an error. Expected output should be
col1|col2|...|col99|col100
MB| GB|...| MB| |MB
MB|GB|...|MB|GB
GB |MB|...|GB|GB
GB|GB|...|MB|GB
GB|MB|...|GB|MB
# My current implementation
df = df.apply(lambda x: x.astype(str).replace('GBs', 'GB').replace('MBs', 'MB').replace('Megabytes', 'MB').replace('Gigabytes', 'GB'))
Can someone guide me a correct and faster way of doing this ?
object(string) dtype?