简体   繁体   English

Python-我想使用pandas将第二行的列移动到第一行列

[英]Python- I want to move second row's column to first row column using pandas

I have csv data like this 我有像这样的csv数据

column1 column2 
A       12
A       13
B       15
B       16
C       12
C       14

I want to merge rows with same value in column 1 by creating another column3 or say transpose like this 我想通过创建另一个column3来合并第1列中具有相同值的行,或者像这样说transpose

column1 column2 column3
A       12      13
B       15      16
C       12      14

I'm using pandas and want to run some loop for this, probably. 我正在使用熊猫,并希望为此运行一些循环。

use groupby with cumcount to set an index then unstack 使用groupbycumcount设置一个索引然后unstack

c = 'column1'
s = df.set_index([c, df.groupby(c).cumcount() + 2]).column2
s.unstack().add_prefix('column').reset_index()

  column1  column2  column3
0       A       12       13
1       B       15       16
2       C       12       14

This will handle if there are uneven numbers of groups as well. 如果组的数量不均匀,这将处理。

Consider the df 考虑一下df

column1 column2 
A       12
A       13
B       15
B       16
B       16
C       12
C       14
C       14
C       14

Then 然后

c = 'column1'
s = df.set_index([c, df.groupby(c).cumcount() + 2]).column2
s.unstack().add_prefix('column').reset_index()

  column1  column2  column3  column4  column5
0       A     12.0     13.0      NaN      NaN
1       B     15.0     16.0     16.0      NaN
2       C     12.0     14.0     14.0     14.0

If you wanted to fill those NaN ahead of time, use the fill_value parameter in unstack 如果你想填补这些NaN时间提前,使用fill_value参数unstack

c = 'column1'
s = df.set_index([c, df.groupby(c).cumcount() + 2]).column2
s.unstack(fill_value=0).add_prefix('column').reset_index()


  column1  column2  column3  column4  column5
0       A       12       13        0        0
1       B       15       16       16        0
2       C       12       14       14       14

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将列名移动到 pandas 框架中的第一行 - move column names to first row in pandas frame 在组的最后一行创建一列,将组的第一行放在第一列,第二列放在第二列......等使用熊猫 - Create a column on the last row of the group that places the first row of group in first column and second in second column…etc using pandas Python Pandas:我无法将第二行设置为列标题 - Python Pandas: I'm unable to set second row as column headers 如果Nan,熊猫会将列值移动到同一列的第一行 - Pandas move column values to first row within same column if Nan 如何在第一列的最后一行下移动熊猫列? - How to move pandas column under the last row of the first column? 在 python pandas 中使用第一行的一部分和第二行的一部分作为列标题 - Use part of first row and part of second row as column headers in python pandas 使用python(熊猫)删除excel中的第一行和第一列 - Deleting first row and column in excel using python (pandas) 我想选择一列每一行的前 4 个单词,并根据该值使用 python 为另一个新创建的列分配一个新值 - I want to to pick the first 4 words of each row of a column and based on the value assign a new value to another newly created column using python 如何使用 Python Pandas 将每行中列的前四个元素和 Append 提取到新创建的列? - How Can I Take The First Four Elements Of A Column In Each Row and Append it To A Newly Created Column Using Python Pandas? 如何在 python pandas 列中添加 1,如 row(n)=row(n-1)+1? - How can I add 1s in a python pandas column like row(n)=row(n-1)+1?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM