我有像在熊猫数据帧下列许多列中的数据:在整个数据帧蟒同时更换多个值
col1| col2| ...| col99 |col100
MBs| Gigabytes|...| MBs| |MBs
Megabytes| GBs|...|Megabytes |Gigabytes
GB | Megabytes| ...|Gigabytes|Gigabytes
GBs | GB |... |MBs |Gigabytes
Gigabytes|Megabytes|...|Gigabytes |Megabytes
我也有它映射类似值的字典。例如,
mapping = {'Megabytes':'MB', 'Gigabytes':'GB', 'MBs':'MB','GBs':'GB', 'GB':'GB',}
我想用字典中的映射值替换列中的每个值。目前我正在尝试做这样的事情,但得到一个错误。预期的输出应该是
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'))
有人能指导我一个正确和快速的方式做到这一点吗?
正在__all__你的列是'object'(串)D型列? – MaxU