[英]Concat/Transpose/Groupby pandas column
So I have the following DataFrame
within pandas:所以我在
DataFrame
中有以下DataFrame
:
Column 1 | Column 2
Name | A
Number | B
Age | C
Name | D
Number | E
Age | F
Each Name, Number and Age grouped togther all relate to one feature and are repeated throughout the dateframe.分组在一起的每个姓名、编号和年龄都与一个功能相关,并在整个日期范围内重复。 I am and wondering what the best method would be to get it in the following format?:
我想知道以以下格式获取它的最佳方法是什么?:
Name | Number | Age
Feature 1 A | B | C
Feature 2 D | E | F
Any help would be appreciated as I'm stumped as to what function or method I would use!任何帮助将不胜感激,因为我不知道我会使用什么功能或方法!
This is a pivot, but you first need to create a label to group the sets of 3 rows together.这是一个数据透视表,但您首先需要创建一个标签以将 3 行的集合组合在一起。 If the data are clean enough such that the DataFrame is always ordered Name, Number, Age, Name, Number, Age, ..., you can
cumsum
a Boolean Series checking which rows are 'Name' to group them together.如果数据足够干净,以至于 DataFrame 始终按名称、编号、年龄、名称、编号、年龄等
cumsum
,您可以cumsum
布尔系列检查哪些行是“名称”以将它们组合在一起。
df['index'] = 'Feature ' + df['Column 1'].eq('Name').cumsum().astype(str)
# Column 1 Column 2 index
#0 Name A Feature 1
#1 Number B Feature 1
#2 Age C Feature 1
#3 Name D Feature 2
#4 Number E Feature 2
#5 Age F Feature 2
df = (df.pivot(index='index', columns='Column 1', values='Column 2')
.rename_axis(index=None, columns=None))
# Age Name Number
#Feature 1 C A B
#Feature 2 F D E
Alternatively you could group every three rows together with integer division based on the length of the DataFrame.或者,您可以根据 DataFrame 的长度将每三行与整数除法组合在一起。
import numpy as np
df['index'] = np.char.add(['Feature '], (np.arange(len(df))//3+1).astype(str))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.