简体   繁体   中英

how to move data from one column to another column's row?

I have following DataFrame

0 1 2 3 4
First row row row row
Second row row row row

beacuse my dataframe can be longer, I want to rename first 3 columns, and then I want the rows for the next 3 columns, to be dropped as a row for the first column

df.rename(columns={0:'data', 1:'user', 2:'file')
data user file 3 4 5
dataa_1 user_1 file_1 dataa_2 user_2 file_2
Second row row row row row

and then I want to write some code, so the rest of 3 column's row would be moved as a second row of my first column:

data user file 3 4 5
dataa_1 user_1 file_1 row row row
dataa_2 user_2 file_2 row row row

Maybe something like this

import pandas as pd

df = pd.DataFrame([range(6),range(6)],columns = ['first','second','third','fourth','fifth','six'])
df2 = df[['fourth','fifth','six']]
df2 = df2.rename(columns = {'fourth':'first','fifth':'second','six':'third'})
df3 = pd.concat([df,df2])

For the following result

在此处输入图像描述

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