我有一个包含列的数据框
Name Date Date_x Date_y A A_x A_y..
我需要将 _z 添加到还没有 _x 或 _y 的列(名称列除外)。所以,我希望输出类似于
Name Date_z Date_x Date_y A_z A_x A_y...
我试过了
df.iloc[:,~df.columns.str.contains('x|y|Name')]=df.iloc[:,~df.columns.str.contains('x|y|Name')].add_suffix("_z")
# doesn't add suffixes and replaces columns with all nans
df.columns=df.columns.map(lambda x : x+'_z' if "x" not in x or "y" not in x else x)
#many variations of this but seems to add _z to all of the column names
最佳答案
怎么样:
df.columns = [x if x=='Name' or '_' in x else x+'_z' for x in df.columns]
关于python - 向尚无后缀的列名称添加后缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58208517/