简体   繁体   中英

How to split df column into df row?

I have a df that looks like this

id shortTextContent shortTextCode longPlainTextContent longTextCode semiTextContent semiTextCode
1  shortContent1         shortCode1         long1                longCode1    semiContent1    semiCode1
2  shortContent2         shortCode2         long2                longCode2    semiContent2    semiCode2

How should I split it into the following content? (where the column names become row content, like below)

id content               content            code
1  shortTextContent      shortContent1      shortCode1
1  longPlainTextContent  long1              longCode1
1  semiTextContent       semiContent1       semiCode1
...
df = pd.DataFrame(dict(id=[1,2,3,4],other=['a','b','c','d']))
df_melted = pd.melt(df)

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