[英]how to move data from one column to another column's row?
I have following DataFrame我有关注 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因为我的 dataframe 可以更长,我想重命名前 3 列,然后我想要接下来的 3 列的行,作为第一列的一行删除
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:然后我想写一些代码,所以 3 列行的 rest 将作为第一列的第二行移动:
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对于以下结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.