简体   繁体   English

如何根据列的值重命名列名

[英]How to rename column names according to value of columns

I need to arrange a Pandas DataFrame with values that aren't in the right columns.我需要安排一个 Pandas DataFrame 的值不在右列中。 I would like to rearrange the values in the cells according to a prefix that I have, and push the 'unknown' columns with their values to the end of the dataframe.我想根据我拥有的前缀重新排列单元格中的值,并将“未知”列及其值推到 dataframe 的末尾。

I have the following dataframe:我有以下 dataframe:

在此处输入图像描述

The output I am looking for is: the 'known' values have a header while the unknowns (5, 6) are to the end.我正在寻找的 output 是:“已知”值具有 header 而未知数(5、6)到最后。 the 'rule': if there is no cell with '|' “规则”:如果没有带有“|”的单元格in the column then the column name will not be changed.在列中,则列名将不会更改。

在此处输入图像描述

any suggestions that I could try would be really helpful in solving this.我可以尝试的任何建议都对解决这个问题非常有帮助。

Try this:尝试这个:

import pandas as pd
rename_dict = {} # reset rename dictionay
df = pd.DataFrame({'1':['name | Steve', 'name | John'],  
                   '2':[None, None],
                    '3':[None , 'age | 50']})
for col in df.columns:
    vals = df[col].values # look at values in each column
    vals =  [x for x in vals if x] # remove Nulls
    vals = [x for x in vals if '|' in x] # leave values with | only
    if len(vals) > 0:
        new_col_name = vals[0].split('|')[0] # getting the new column name
        rename_dict[col] = new_col_name # add column names to rename dictionay
df.rename(columns=rename_dict, inplace = True) # renaming the column name
df

          name      2      age 
0  name | Steve  None      None
1   name | John  None  age | 50

it looks a bit tricky and not exactly what you expected, but it might give you an idea how to solve your task:它看起来有点棘手,并不完全符合您的预期,但它可能会让您了解如何解决您的任务:

df = pd.DataFrame([['email | 1@mail.com','name | name1','surname | surname1','','',''],
                   ['email | 2@mail.com','','name | name2','occupation | student','surname | surname2','abc | 123']])


df.apply(lambda x: pd.Series(dict([tuple(i.split(' | ')) for i in x.tolist() if i])),axis=1)

>>> out
'''
   abc       email   name occupation   surname
0  NaN  1@mail.com  name1        NaN  surname1
1  123  2@mail.com  name2    student  surname2

You can try this solution:你可以试试这个解决方案:

my_dict = {}
def createDict(ss):
    for i in range(1, 7, 1):
        sss = ss[i].split('|')
        if len(sss) > 1:
            if sss[0].strip() in my_dict:
                my_dict[sss[0].strip()].append(ss[i])
            else:
                my_dict[sss[0].strip()] = [ss[i]]


df = df.apply(lambda x: createDict(x), axis=1)
dff = pd.DataFrame.from_dict(my_dict, orient='index')
dff = dff.transpose()

print(dff)

Hope this answers your question.希望这能回答你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM