将当前列名更改为行,并替换为其他列名

时间:2019-11-19 10:17:44

标签: python pandas dataframe

大家好,我有一个凌乱的数据框,其中的行值显示为列名。现在,我要做的就是将那些以列名形式出现的行值更改为仅仅是行,并用其他列名替换它们。这是原始数据帧的样子:

# dictionary of lists 
dict = {'Erick':["aparna", "pankaj", "sudhir", "Geeku"], 
        'MBA': ["MBA", "BCA", "M.Tech", "MBA"], 
        '80':[90, 40, 80, 98]} 

df = pd.DataFrame(dict) 

print(df)

现在,我想将那些列名更改为一行,并用新的列名替换 这是预期的输出

# dictionary of lists 
dict = {'Name':["Erick","aparna", "pankaj", "sudhir", "Geeku"], 
        'Degree': ["MBA","MBA", "BCA", "M.Tech", "MBA"], 
        'Score':[80,90, 40, 80, 98]} 

df = pd.DataFrame(dict) 

print(df)

请帮助

3 个答案:

答案 0 :(得分:1)

一个想法是从列和DataFrame.append原始数据中创建1行DataFrame

df = df.columns.to_series().to_frame().T.append(df, ignore_index=True)
df.columns = ['Name','Degree','Score']
print(df)
     Name  Degree Score
0   Erick     MBA    80
1  aparna     MBA    90
2  pankaj     BCA    40
3  sudhir  M.Tech    80
4   Geeku     MBA    98

或使用setting with enlargement

df.loc[-1] = df.columns
df = df.sort_index().reset_index(drop=True)
df.columns = ['Name','Degree','Score']
print(df)
     Name  Degree Score
0   Erick     MBA    80
1  aparna     MBA    90
2  pankaj     BCA    40
3  sudhir  M.Tech    80
4   Geeku     MBA    98

或通过构造函数通过字典创建DataFrame列来创建rename

#change dict in DataFrame constructor and reset builtins for avoid
#TypeError: 'dict' object is not callable
import builtins
dict = builtins.dict

d = {'Erick':["aparna", "pankaj", "sudhir", "Geeku"], 
        'MBA': ["MBA", "BCA", "M.Tech", "MBA"], 
        '80':[90, 40, 80, 98]} 

df = pd.DataFrame(d) 


c = ['Name','Degree','Score']
df = pd.DataFrame([df.columns], columns=c).append(df.rename(columns=dict(zip(df.columns, c))), 
                  ignore_index=True)
print(df)
     Name  Degree Score
0   Erick     MBA    80
1  aparna     MBA    90
2  pankaj     BCA    40
3  sudhir  M.Tech    80
4   Geeku     MBA    98

答案 1 :(得分:0)

您还可以尝试以下代码来获得所需的输出:

dict = {'Erick':["aparna", "pankaj", "sudhir", "Geeku"], 
        'MBA': ["MBA", "BCA", "M.Tech", "MBA"], 
        '80':[90, 40, 80, 98]}

df = (pd.DataFrame(dict).T.reset_index().T.reset_index()).drop(['index'],axis=1)
df.columns = ['Name','Degree','Score']
print(df)

请告诉我该代码是否适合您。

答案 2 :(得分:0)

这是一个非常具体的问题,所以我虽然有一个非常巧妙的答案,但是我还是尝试了。

考虑到您有另一个按正确顺序排列的键,可以执行以下操作:

dict = {'Erick':["aparna", "pankaj", "sudhir", "Geeku"], 
        'MBA': ["MBA", "BCA", "M.Tech", "MBA"], 
        '80':[90, 40, 80, 98]} 

new_dict_keys = ['Name', 'Degree', 'Score']
new_dict = {}

for i, key in enumerate(dict.keys()):
    try:
        dict[key].append(int(key))
    except Exception as e:
        dict[key].append(key)

    new_dict[new_dict_keys[i]] = dict[key]

print(new_dict)

请记住,您的new_dict_keys数组必须以正确的顺序运行。 否则,您也可以这样做:

new_dict = {'Name': 'Erick', 'Degree': 'MBA', 'Score': '80'}

for i, key in enumerate(new_dict.keys()):
    try:
        dict[new_dict[key]].append(int(new_dict[key]))
    except Exception as e:
        dict[new_dict[key]].append(new_dict[key])

    new_dict[key] = dict[new_dict[key]]

print(new_dict)

两者都返回您所需的输出:

{
'Name': ['aparna', 'pankaj', 'sudhir', 'Geeku', 'Erick'], 
'Degree': ['MBA', 'BCA', 'M.Tech', 'MBA', 'MBA'], 
'Score': [90, 40, 80, 98, 80]
}

由您决定如何包括新的键名。

最后一件事:不要dict作为变量名,它是python关键字,因此永远不要用作变量名。