如何使用其他行的信息组合更改列名?

时间:2017-12-28 13:19:34

标签: python pandas

enter image description here

每个偶数列在我的数据框中都有一个名称“sales.xxxxxxxx”。

我想在列之前阅读,这(奇数)列只包含一个信息--3个唯一的数字代码,例如AAA,AAB ......等。

我想将偶数列的名称更改为“AAA.xxxxxxx”,只需替换字符串“sales”。

我怎么能这样做?

我尝试过:

首先,我放弃了第一栏(我不需要)

del df["Unnamed: 0"]

其次,我已经将每个cate_level列放入列表中,只是将一个字符串元素作为循环语句(但我认为它在计算效率上也非常低效。它需要太长时间)然后,与下一列名称连接。

for i in range(1, 6905, 2): #there exist 6905 columns, so loop over every 2 columns
    for k in range(11293): #each column has 11293 rows
        tri_codes = df[str(df.columns[i])].tolist() #check which tri_code that cate column have
        if type(tri_codes[k]) == str: #once catch the string
            column_cat = tri_codes[k]
            break #done, quickly finish the loop
    df.rename(columns={ df.columns[i+1] : column_cat + "."+ df.columns[i+1].split(".")[1]}) #change col name

添加。我尝试做什么: 将每个sales.xxxx column_name更改为tricode.xxxx,如下所示: enter image description here

1 个答案:

答案 0 :(得分:0)

您可以遍历列名称,使用dropnailoccate_level列获取非Nan值并构建字典。该字典可以传递给rename函数来重命名列。它看起来像这样:

import pandas as pd
from numpy import nan

df = pd.DataFrame({'customer_code': [1023506, 1036996, 1072231, 1003937, 1048230, 1010773],
                   'cate_level_1.22212113': ['AHA', 'AHA', nan, 'AHA', 'AHA', 'AHA'],
                   'sales.22212113': [15065904, 2317445, 0, 1078560, 4848960, 252720],
                   'cate_level_1.22212118': ['AHA', 'AHA', nan, 'AHA', 'AHA', 'AHA'],
                   'sales.22212118': [13837150, 1247550, 9435310, 1080660, 2160528, 2609750]})

df = df[['customer_code', 'cate_level_1.22212113', 'sales.22212113', 'cate_level_1.22212118', 'sales.22212118']]

dic = {}
for col in df:
    if col.startswith('cate'):
        num = col[12:]
        dic['sales' + num] = df[col].dropna().iloc[0] + num

print('Input DF:\n' + str(df))
df.rename(mapper=dic, axis='columns', inplace=True)
print('\nOutput DF:\n' + str(df))

结果如下:

Input DF:
   customer_code cate_level_1.22212113  sales.22212113 cate_level_1.22212118  sales.22212118
0        1023506                   AHA        15065904                   AHA        13837150
1        1036996                   AHA         2317445                   AHA         1247550
2        1072231                   NaN               0                   NaN         9435310
3        1003937                   AHA         1078560                   AHA         1080660
4        1048230                   AHA         4848960                   AHA         2160528
5        1010773                   AHA          252720                   AHA         2609750


Output DF:
   customer_code cate_level_1.22212113  AHA.22212113 cate_level_1.22212118  AHA.22212118
0        1023506                   AHA      15065904                   AHA      13837150
1        1036996                   AHA       2317445                   AHA       1247550
2        1072231                   NaN             0                   NaN       9435310
3        1003937                   AHA       1078560                   AHA       1080660
4        1048230                   AHA       4848960                   AHA       2160528
5        1010773                   AHA        252720                   AHA       2609750