简体   繁体   中英

Convert single row into pandas column

I have similar dataframe files as given simple dataframe. I wanted to selected single row of data and convert into rearranged column.

Created problem dataframe is df1:

import pandas as pd

data =[['Name','john','riya','alex'],
       ['Age',28, 24, 34],[ 'Month','February','January','March'],
       ['Status','M','F','M']] 

df = pd.DataFrame(data,columns=['index',0,0,0])
df1 = df.set_index('index')

I would like to rearrange dataframe as df2 as given below:

desired_data = {'January' :pd.Series(['riya', 24, 'F'], index=['Name', 'Age','Status']),
                'February':  pd.Series(['john', 28, 'M'], index=['Name', 'Age','Status']),
                'March' : pd.Series(['alex', 34, 'M'], index=['Name', 'Age','Status'])}

df2 = pd.DataFrame(desired_data)

Use ordered categoricals for original sorted values:

cats = ['January',
 'February',
 'March',
 'April',
 'May',
 'June',
 'July',
 'August',
 'September',
 'October',
 'November',
 'December']

df1 = (df1.set_axis(pd.Categorical(df1.loc['Month'], ordered=True, categories=cats), axis=1)
          .drop('Month')
          .sort_index(axis=1)
          .rename_axis(index=None, columns=None))
print (df1)
      January February March
Name      riya     john  alex
Age         24       28    34
Status       F        M     M

Or create dictionary used for sorting:

cats = ['January',
 'February',
 'March',
 'April',
 'May',
 'June',
 'July',
 'August',
 'September',
 'October',
 'November',
 'December']

d = {v: k for k, v in dict(enumerate(cats)).items()}
print (d)
{'January': 0, 'February': 1, 'March': 2, 'April': 3, 'May': 4, 'June': 5, 'July': 6,
 'August': 7, 'September': 8, 'October': 9, 'November': 10, 'December': 11}

df1 = (df1.set_axis(df1.loc['Month'], axis=1)
          .drop('Month')
          .reindex(sorted(df1.loc['Month'], key=d.get), axis=1)
          .rename_axis(index=None, columns=None))

print (df1)
       January February March
Name      riya     john  alex
Age         24       28    34
Status       F        M     M

Thank you @SeaBean for another solution:

df1 = (df1.set_axis(df1.loc['Month'], axis=1)
          .drop('Month')
          .sort_index(axis=1, key=lambda x: pd.to_datetime(x, format='%B').month)
          .rename_axis(index=None, columns=None))
df1.columns = df1.iloc[2]
df1 = df1.drop(df1.index[2])
df1 = df1.rename_axis(None).rename_axis(None,axis='columns')
df1

Output

        February    January March
Name    john        riya    alex
Age     28          24      34
Status  M           F       M

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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