Pandas:遍历数据框并根据条件应用更改。

huangapple go评论80阅读模式
英文:

Pandas:iterate through the dataframe and apply changes with conditions

问题

我必须遍历列= 0,如果我在列= 0中找到任何整数,如2010、2018、2017等,我必须将其分配给列= 0中所有的值作为年份。

我的DF:
0 1
Nan 银行
国家银行 轴银行
Nan ICICI
Nan PNB
2010 KYB
Nan Indus Ind
Nan 卡尔

我的期望输出:
0 1
2010 银行
2010 轴银行
2010 ICICI
2010 PNB
2010 KYB
2010 Indus Ind
2010 卡尔

英文:

I have to iterate through column=0 and if I find any integer like 2010,2018,2017 etc in my column =0, I have to assign that to all the values in column=0 as year.

PS:-column=0 is an object datatype.

My DF:
0 1
Nan Banks
National Banks Axis Bank
Nan ICICI
Nan PNB
2010 KYB
Nan Indus Ind
Nan Karur

My desired output:
0 1
2010 Banks
2010 Axis Bank
2010 ICICI
2010 PNB
2010 KYB
2010 Indus Ind
2010 Karur

答案1

得分: 1

将列转换为数值类型使用 to_numeric,然后使用 Series.whereSeries.isin 将所有不在 range 范围内的数字转换为缺失值 NaN

s = pd.to_numeric(df[0], errors='coerce')
df[0] = s.where(s.isin(range(2010, 2020)))
print(df)
            0          1
0     NaN      Banks
1     NaN  Axis Bank
2     NaN      ICICI
3     NaN        PNB
4  2010.0        KYB
5     NaN  Indus Ind
6     NaN      Karur
英文:

Convert column to numeric by to_numeric and then convert all numbers outside range to missing values to NaNs by Series.where with mask by Series.isin:

s = pd.to_numeric(df[0], errors='coerce')
df[0] = s.where(s.isin(range(2010, 2020)))
print (df)
        0          1
0     NaN      Banks
1     NaN  Axis Bank
2     NaN      ICICI
3     NaN        PNB
4  2010.0        KYB
5     NaN  Indus Ind
6     NaN      Karur

huangapple
  • 本文由 发表于 2020年1月3日 16:47:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575494.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定