如何根据条件更改pd.DataFrame的值?

时间:2018-12-27 19:39:13

标签: python pandas

我有FIFA数据集,其中包含有关足球运动员的信息。该数据集的特征之一是足球运动员的价值,但它是字符串形式,例如“ $ 300K”或“ $ 50M”。我该如何简单地删除这些欧元和“ M,K”符号,并以相同的单位写出它们的值?

import numpy as np
import pandas as pd

location = r'C:\Users\bemrem\Desktop\Python\fifa\fifa_dataset.csv'

_dataframe = pd.read_csv(location)

_dataframe = _dataframe.dropna()
_dataframe = _dataframe.reset_index(drop=True)
_dataframe = _dataframe[['Name', 'Value', 'Nationality', 'Age', 'Wage', 
'Overall', 'Potential']]

_array = ['Belgium', 'France', 'Brazil', 'Croatia', 'England',' Portugal', 
'Uruguay', 'Switzerland', 'Spain', 'Denmark']

_dataframe = _dataframe.loc[_dataframe['Nationality'].isin(_array)]
_dataframe = _dataframe.reset_index(drop=True) 


print(_dataframe.head())
print()
print(_dataframe.tail())

我尝试转换此“值”列,但失败了。这就是我得到的

           Name   Value Nationality  Age   Wage  Overall  Potential
0        Neymar   €123M      Brazil   25  €280K       92         94
1     L. Suárez    €97M     Uruguay   30  €510K       92         92
2     E. Hazard  €90.5M     Belgium   26  €295K       90         91
3  Sergio Ramos    €52M       Spain   31  €310K       90         90
4  K. De Bruyne    €83M     Belgium   26  €285K       89         92

              Name Value Nationality  Age Wage  Overall  Potential
4931    A. Kilgour  €40K     England   19  €1K       47         56
4932      R. White  €60K     England   18  €2K       47         65
4933     T. Sawyer  €50K     England   18  €1K       46         58
4934     J. Keeble  €40K     England   18  €1K       46         56
4935  J. Lundstram  €60K     England   18  €1K       46         64

但是我想我的输出看起来像这样:

           Name   Value Nationality  Age   Wage  Overall  Potential
0        Neymar   123      Brazil   25  €280K       92         94
1     L. Suárez    97     Uruguay   30  €510K       92         92
2     E. Hazard  90.5     Belgium   26  €295K       90         91
3  Sergio Ramos    52       Spain   31  €310K       90         90
4  K. De Bruyne    83     Belgium   26  €285K       89         92

              Name Value Nationality  Age Wage  Overall  Potential
4931    A. Kilgour  0.04     England   19  €1K       47         56
4932      R. White  0.06     England   18  €2K       47         65
4933     T. Sawyer  0.05     England   18  €1K       46         58
4934     J. Keeble  0.04     England   18  €1K       46         56
4935  J. Lundstram  0.06     England   18  €1K       46         64

1 个答案:

答案 0 :(得分:0)

我的信誉不足,无法将答案标记为重复。但是,我相信,如果字符串中没有“ K”或“ M”,那么除了提供解决方案之外,这还将解决您的特定问题。

您还需要在正则表达式中将$替换为

Convert the string 2.90K to 2900 or 5.2M to 5200000 in pandas dataframe