0
我正在导入一个数据集与Python的熊猫,不幸需要一些清洁。导入后,我需要删除两列中的所有引号和空格(alpha2
和alpha3
)。这是目前我如何做到这一点:在使用熊猫导入CSV文件时有效地清理数据
# Add alpha2 country codes to custom dataset to normalize data
country_codes = pd.read_csv('datasets/country_codes.csv').rename(columns = {'Alpha-2 code':'alpha2', 'Alpha-3 code':'alpha3'})
# Remove commas and spaces from dataset
country_codes['alpha2'] = country_codes['alpha2'].str.replace('"', '')
country_codes['alpha2'] = country_codes['alpha2'].str.replace(' ', '')
country_codes['alpha3'] = country_codes['alpha3'].str.replace('"', '')
country_codes['alpha3'] = country_codes['alpha3'].str.replace(' ', '')
在我oppinion,这是一个有点难看,因为我需要一些简单的命令5条规则。这可以通过更少的代码更有效地完成吗?
在链式表达式中使用inplace = True是不惯用的,它可能仅在有时;而只是简单地返回新的值 – Jeff