简体   繁体   English

如果满足条件,则在Pandas数据框中重命名列

[英]Renaming column in pandas dataframe if condition is met

I am trying to figure out if it is possible to rename a column header if some condition is met. 我试图找出如果满足某些条件是否可以重命名列标题。 I have a pandas dataframe: 我有一个熊猫数据框:

import pandas as pd

data = [[1,'Joe', '2018', 5,7,9]]

df = pd.DataFrame(data, columns = ['ID', 'Name', 'Year', 'FallScore', 'WinterScore', 'SpringScore'])

print(df)
   ID Name  Year  FallScore  WinterScore  SpringScore
0   1  Joe  2018          5            7            9

What I am trying to do is if a column contains 'Fall', 'Winter', or 'Spring' in it add the string 'NEW' to the end of the column header. 我想做的是,如果一列中包含“秋天”,“冬天”或“春天”,则将字符串“ NEW”添加到列标题的末尾。 Here is what the desired output would look like. 这是所需的输出结果。

df_desired = pd.DataFrame(data, columns = ['ID', 'Name', 'Year', 'FallScoreNEW', 'WinterScoreNEW', 'SpringScoreNEW'])

print(df_desired)
   ID Name  Year  FallScoreNEW  WinterScoreNEW  SpringScoreNEW
0   1  Joe  2018             5               7               9

I was able to get an array of boolean values for each term but have not been able to figure out how to replace the column header if it 'Fall', 'Winter', or 'Spring' is found. 我能够为每个术语获取一个布尔值数组,但是如果找到“秋天”,“冬天”或“春天”,就无法弄清楚如何替换列标题。 Please note I will not know the actual columns headers I will only know that they will contain 'Fall', 'Winter', or 'Spring' 请注意,我将不知道实际的列标题,而只会知道它们将包含“秋季”,“冬季”或“春季”

The way I got an array of boolean values is here: 我得到布尔值数组的方式是在这里:

df.columns.str.contains('Fall')
df.columns.str.contains('Winter')
df.columns.str.contains('Spring')

Any help would be greatly appreciated! 任何帮助将不胜感激!

You can use rename with a lambda: 您可以对lambda使用rename

df.rename(lambda x: x + 'NEW' if any(k in x for k in keys) else x, axis=1)

   ID Name  Year  FallScoreNEW  WinterScoreNEW  SpringScoreNEW
0   1  Joe  2018             5               7               9

This will work as a list comprehension as well, if you don't want a copy of the data returned just for a rename operation. 如果您不希望仅为重命名操作返回数据的副本,这也将用作列表理解。

df.columns = [x + 'NEW' if any(k in x for k in keys) else x for x in df]
df

   ID Name  Year  FallScoreNEW  WinterScoreNEW  SpringScoreNEW
0   1  Joe  2018             5               7               9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM