英文:
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.where
和 Series.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 NaN
s 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论