简体   繁体   中英

Convert columns to rows by groups

I have a data frame like this

Chennai
6200SqFT
10,000 Population
Mumbai
5000sqFT
17,000 Population

I want to convert like this

Chennai    6200SqFT    10,000 Population
Mumbai     5000SqFT    17,000 Population

IIUC, you can below approach:

Assuming your dataframe looks like below:

print(df)
                   0
0            Chennai
1           6200SqFT
2  10,000 Population
3             Mumbai
4           5000sqFT
5  17,000 Population

Solution with np.reshape

output  = pd.DataFrame(df[0].to_numpy().reshape(-1,3))
#or output  = pd.DataFrame(df[0].values.reshape(-1,3))

Output:

         0         1                  2
0  Chennai  6200SqFT  10,000 Population
1   Mumbai  5000sqFT  17,000 Population

Incase you have uneven lines (not a multiple of 3, try):

output = pd.concat([g.reset_index(drop=True) 
         for _,g in df.groupby(df.index//3)],axis=1).T.reset_index(drop=True)

Try the code below,

df_new = pd.DataFrame(df.values.reshape(-1,3), columns=['town', 'area', 'population'])
df_new.show()

Output

    town     area           population
0   Chennai 6200SqFT    10,000 Population
1   Mumbai  5000sqFT    17,000 Population

try this as someone mention using slicing in comment earlier

>>> a,b,c = df[::3].values.reshape(-1), df[1::3].values.reshape(-1), df[2::3].values.reshape(-1)

>>> pd.DataFrame({'a':a,'b':b,'c':c}, index=range(len(a)))
         a         b                  c
0  Chennai  6200SqFT  10,000 Population
1   Mumbai  5000sqFT  17,000 Population

output = pd.concat([g.reset_index(drop=True) for _,g in df.groupby(df.index//3)],axis=1).T.reset_index(drop=True)

Posted by anky_91

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