Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 删除Pandas中某些列名的后缀_Python_Pandas - Fatal编程技术网

Python 删除Pandas中某些列名的后缀

Python 删除Pandas中某些列名的后缀,python,pandas,Python,Pandas,这是我的df: df = pd.DataFrame({'a_x':[1, 2, 3], 'b_x':[20, 30, 40], 'c_x':[10, 40, 50]}) 如何删除第一列和第三列名称的后缀 我不想要这种方法: df=df.rename(列={'a_x':'a','c_x':'c'})一个接一个地对它们进行硬编码 编辑1:我有要删除后缀的列列表。在这种情况下,我有['a','c'] 我期望的结果如下所示: a b_x c 0 1 20 10 1

这是我的df:

df = pd.DataFrame({'a_x':[1, 2, 3], 'b_x':[20, 30, 40], 'c_x':[10, 40, 50]})
如何删除第一列和第三列名称的后缀

我不想要这种方法:
df=df.rename(列={'a_x':'a','c_x':'c'})
一个接一个地对它们进行硬编码

编辑1:我有要删除后缀的列列表。在这种情况下,我有
['a','c']

我期望的结果如下所示:

    a    b_x   c
0    1   20   10
1    2   30   40
2    3   40   50

您可以定义COL并重命名:

>>> target_cols = ['c']
>>> suffix = '_x'
>>> df.rename(columns={col + suffix: col for col in target_cols})

   a_x  b_x   c
0    1   20  10
1    2   30  40
2    3   40  50

# or
# df.rename(columns=dict(zip((col + suffix for col in target_cols), target_cols)))

您可以定义COL并重命名:

>>> target_cols = ['c']
>>> suffix = '_x'
>>> df.rename(columns={col + suffix: col for col in target_cols})

   a_x  b_x   c
0    1   20  10
1    2   30  40
2    3   40  50

# or
# df.rename(columns=dict(zip((col + suffix for col in target_cols), target_cols)))

我想简单的
if/else
和列表理解就可以了

import pandas as pd 

trg_cols = ['a_x','c_x']
new_cols = [col.split('_')[0] if col in trg_cols else col  for col in df.columns]


或者,如果要按输出目标列进行选择

trg_cols = ['a','c']
new_cols = [col if not col.split('_')[0] in trg_cols else col.split('_')[0] 
            for col in df.columns]


df.columns = new_cols
print(df)
   a  b_x   c
0  1   20  10
1  2   30  40
2  3   40  50

使用
fillna
pd.Series

trg_cols = ['a','c']
s = pd.Series(df.columns)
df.columns = s.str.extract('('+'|'.join(trg_cols)+')',expand=False).fillna(s)


print(df)
   a  b_x   c
0  1   20  10
1  2   30  40
2  3   40  50

我想简单的
if/else
和列表理解就可以了

import pandas as pd 

trg_cols = ['a_x','c_x']
new_cols = [col.split('_')[0] if col in trg_cols else col  for col in df.columns]


或者,如果要按输出目标列进行选择

trg_cols = ['a','c']
new_cols = [col if not col.split('_')[0] in trg_cols else col.split('_')[0] 
            for col in df.columns]


df.columns = new_cols
print(df)
   a  b_x   c
0  1   20  10
1  2   30  40
2  3   40  50

使用
fillna
pd.Series

trg_cols = ['a','c']
s = pd.Series(df.columns)
df.columns = s.str.extract('('+'|'.join(trg_cols)+')',expand=False).fillna(s)


print(df)
   a  b_x   c
0  1   20  10
1  2   30  40
2  3   40  50

这适用于python3.8和;因为您已经有了列的列表:

df.rename(columns = lambda x: val if (val:=x.split("_")[0]) in ["a", "c"] else x)

   a  b_x   c
0  1   20  10
1  2   30  40
2  3   40  50

这适用于python3.8和;因为您已经有了列的列表:

df.rename(columns = lambda x: val if (val:=x.split("_")[0]) in ["a", "c"] else x)

   a  b_x   c
0  1   20  10
1  2   30  40
2  3   40  50

为什么不呢?如果您知道每列的后缀,并且使用循环构造映射,那么重命名就可以了。为什么不呢?如果您知道每个列的后缀,并且使用循环构造映射,则可以重命名。