pandas 为具有特定重复列名的数据框添加后缀

ldioqlga  于 2023-06-04  发布在  其他
关注(0)|答案(3)|浏览(421)

我在数据框中有数据,如以下列:周、部位、LAL、部位、LAL。我需要给col name == 'SITE'分配一个后缀,这样最终的df看起来像这样:周、SITE_1、LAL、SITE_2、LAL。
谢谢你
Dataframe 示例:

week    SITE        LAL SITE        LAL
0   1   BARTON CHAPEL   1.1 PENASCAL I  1.0
1   2   BARTON CHAPEL   1.1 PENASCAL I  1.0
2   3   BARTON CHAPEL   1.1 PENASCAL I  1.0
3   4   BARTON CHAPEL   1.1 PENASCAL I  1.0
4   5   BARTON CHAPEL   1.1 PENASCAL I  1.0
5   6   BARTON CHAPEL   1.4 PENASCAL I  1.0
qojgxg4l

qojgxg4l1#

您可以尝试使用itertools.count

from itertools import count

cnt = count(1)
df.columns = [f'{c}_{next(cnt)}' if c == 'SITE' else c for c in df.columns]

print(df)

图纸:

week         SITE_1  LAL      SITE_2  LAL
0     1  BARTON CHAPEL  1.1  PENASCAL I  1.0
1     2  BARTON CHAPEL  1.1  PENASCAL I  1.0
2     3  BARTON CHAPEL  1.1  PENASCAL I  1.0
3     4  BARTON CHAPEL  1.1  PENASCAL I  1.0
4     5  BARTON CHAPEL  1.1  PENASCAL I  1.0
5     6  BARTON CHAPEL  1.4  PENASCAL I  1.0
t2a7ltrp

t2a7ltrp2#

另一种可能的解决方案:

df.columns = [
    f'{x}_{y}' if x == 'SITE' else x for x, y in 
    zip(df.columns, np.cumsum(df.columns == 'SITE'))]

输出:

week         SITE_1  LAL      SITE_2  LAL
0     1  BARTON CHAPEL  1.1  PENASCAL I  1.0
1     2  BARTON CHAPEL  1.1  PENASCAL I  1.0
2     3  BARTON CHAPEL  1.1  PENASCAL I  1.0
3     4  BARTON CHAPEL  1.1  PENASCAL I  1.0
4     5  BARTON CHAPEL  1.1  PENASCAL I  1.0
5     6  BARTON CHAPEL  1.4  PENASCAL I  1.0
ffvjumwh

ffvjumwh3#

另一种选择是创建一个字典并使用rename:

d = {'SITE':list(range(1,df.columns.tolist().count('SITE')+1))}

df.rename(lambda x: x + '_' + str(d.get(x).pop(0)) if x in d else x,axis=1)

相关问题