小编Lui*_*Gan的帖子

Python Pandas df,将货币中的 $、M 和 K 替换为 int 的最佳方法

我正在做一个个人项目来练习熊猫和美丽的汤,我抓取了这个信息并将它放在一个熊猫 df 中,如下所示:

0        €8.5M
1           €0
2        €9.5M
3          €2M
4         €21M
         ...  
16534    €1.8M
16535    €1.1M
16536    €550K
16537    €650K
16538    €1.1M
Name: Value, Length: 16539, dtype: object
0        €67K
1          €0
2        €15K
3        €11K
4        €13K
         ... 
16534     €3K
16535     €2K
16536     €2K
16537     €7K
16538     €3K
Name: Wage, Length: 16539, dtype: object  
Run Code Online (Sandbox Code Playgroud)

所以为了分析这些信息,我想清理这些数据并将其转换为整数,我能想到的是:

df['Wage'] = df['Wage'].apply(lambda x: re.sub('€','',x))
df['Wage'] = df['Wage'].apply(lambda x: re.sub('K','000',x))

df['Value'] = df['Value'].apply(lambda x: re.sub('€','',x))
df['Value'] = df['Value'].apply(lambda x : re.sub('M','00000',x) if …
Run Code Online (Sandbox Code Playgroud)

python regex dataframe pandas

3
推荐指数
1
解决办法
77
查看次数

标签 统计

dataframe ×1

pandas ×1

python ×1

regex ×1