我有这样一个pandas数据框架,
Name Not_Included Quantity Not_Included
0 Auto DNS 10 DNS
1 NaN DNS 12 DNS
2 Rtal DNS 18 DNS
3 NaN DNS 14 DNS
4 Indl DNS 16 DNS
5 NaN DNS 18 DNS现在,我希望使用数据帧的列索引重命名Not_Included。所以,我得到这样的输出,
Name Not_Included_1 Quantity Not_Included_3
0 Auto DNS 10 DNS
1 NaN DNS 12 DNS
2 Rtal DNS 18 DNS
3 NaN DNS 14 DNS
4 Indl DNS 16 DNS
5 NaN DNS 18 DNS我试过以下几种方法
for c,v in enumerate(s_df):
if v == 'Not_Included':
vi = 'Not_Included' + str(c)
s_df.rename(columns=lambda n: n.replace(v, vi), inplace=True)我得到了以下结果,
Name Not_Included31 Quantity Not_Included31
0 Auto DNS 10 DNS
1 NaN DNS 12 DNS
2 Rtal DNS 18 DNS
3 NaN DNS 14 DNS
4 Indl DNS 16 DNS
5 NaN DNS 18 DNS有posts来重命名整个数据帧的列,但这不是我想要的,因为我正在自动化一些任务。如何使用列索引获得所需的输出?
另外,我可以用列表理解的方法来重命名熊猫栏吗?
任何想法都会很好。
发布于 2019-02-15 21:32:40
可以使用np.where设置列,检查其复制的位置。
import numpy as np
df.columns = np.where(df.columns.duplicated(),
[f'{df.columns[i]}_{i}' for i in range(len(df.columns))],
df.columns)索引还有一个where方法:
df.columns = df.columns.where(~df.columns.duplicated(),
[f'{df.columns[i]}_{i}' for i in range(len(df.columns))])输出:
Name Not_Included Quantity Not_Included_3
0 Auto DNS 10 DNS
1 NaN DNS 12 DNS
2 Rtal DNS 18 DNS发布于 2019-02-15 21:45:51
这也能起作用
df.columns = ['{}_{}'.format(coluna, index) if 'Not_Included' in coluna else coluna for index, coluna in enumerate(df.columns)]https://stackoverflow.com/questions/54717242
复制相似问题