Gin*_*aze 2 python python-3.x pandas
我有一个数据框,其中包含类似的列
Name Date Date_x Date_y A A_x A_y..
Run Code Online (Sandbox Code Playgroud)
我需要将 _z 添加到还没有 _x 或 _y 的列(名称列除外)。所以,我希望输出类似于
Name Date_z Date_x Date_y A_z A_x A_y...
Run Code Online (Sandbox Code Playgroud)
我试过了
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
Run Code Online (Sandbox Code Playgroud)
怎么样:
df.columns = [x if x=='Name' or '_' in x else x+'_z' for x in df.columns]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
627 次 |
| 最近记录: |