Pandas wide_to_long(~) 方法将 DataFrame 的格式从宽格式转换为长格式。
让我们看一个简单的例子。假设我们有以下 DataFrame:
|
名字 |
年龄 |
高度 |
|---|---|---|
|
alex |
40 |
150 |
|
bob |
50 |
160 |
这被认为是一个宽的DataFrame 因为每一行都捕获有关该人的所有相关数据。现在,将其转换为长 DataFrame :
|
名字 |
变量 |
值 |
|---|---|---|
|
alex |
age |
30 |
|
alex |
height |
150 |
|
bob |
age |
50 |
|
bob |
height |
160 |
现在,每一行捕获有关该人的单个变量,这不可避免地会导致垂直长 DataFrame 。
Pandas 使用这个术语"unpivot"表示基于变量延长 DataFrame 的操作。在这个例子中,我们是不旋转变量age和height.
参数
1. df | DataFrame
输入数据帧。
2. stubnames | string 或 list-like
将拉长的列标签的前缀。
3. i | string 或 list-like
用作标识符的列。
4. j | string
分配给新引入的列的名称。
5. sep | string | optional
例如,如果您希望延长的列具有标签 "A_2012" ,则您可以设置 stubnames="A" 和 sep="_" ,而不是将 "A_" 指定为 stubnames 。默认情况下,sep="" 。
6. suffix | string | optional
与要延长的列后缀匹配的字符串或正则表达式。默认情况下,suffix="\d+" 。
返回值
列已取消透视的DataFrame。
例子
考虑以下 DataFrame :
df = pd.DataFrame({"name":["alex","bob","cathy"], "savings_2019":[10,20,30], "savings_2020":[40,50,60]})
df
name savings_2019 savings_2020
0 alex 10 40
1 bob 20 50
2 cathy 30 60
基本用法
从宽DataFrame变为长DataFrame:
pd.wide_to_long(df, stubnames=["savings_"], i="name", j="year")
savings_
name year
alex 2019 10
bob 2019 20
cathy 2019 30
alex 2020 40
bob 2020 50
cathy 2020 60
在这里,请注意下划线是如何包含在存根名称中的("savings_")。这是因为如果没有 _ ,该方法将使用 "_2019" 之类的后缀,而不仅仅是数字 "2019" 来查找要取消透视的列标签。这是一个问题,因为默认的 suffix 参数 ( "\d+" ) 仅允许数字。
这种方法的缺点是新的列名变成了 savings_ ,这无疑很尴尬。
指定 sep 参数
我们可以让 stubnames 为 "saving" 并设置 sep="_" 来获得相同的行为,而不是 "saving_" 。这样做的优点是我们的列名称现在是 savings :
pd.wide_to_long(df, stubnames=["savings"], i="name", j="year", sep="_")
savings
name year
alex 2019 10
bob 2019 20
cathy 2019 30
alex 2020 40
bob 2020 50
cathy 2020 60
指定后缀参数
考虑以下 DataFrame :
df = pd.DataFrame({"name":["alex","bob","cathy"], "savings_2019A":[10,20,30], "savings_2020A":[40,50,60]})
df
name savings_2019A savings_2020A
0 alex 10 40
1 bob 20 50
2 cathy 30 60
这里,这个DataFrame和之前的DataFrame的区别在于每个列名都附加了一个"A"字符。
如果我们像上一个例子一样调用wide_to_long(~),结果将为空:
pd.wide_to_long(df, ["savings_"], i="name", j="year")
savings_2019A savings_2020A savings_
name year
这是因为列名称现在是 "2019A" 而不是数字 "2019" 。由于后缀的默认值仅允许数字 ( "\d+" ),因此列 "2019A" 不会取消透视。我们可以通过设置正则表达式 "\w+" 来解决此问题,它允许使用 "2019A" 等术语:
pd.wide_to_long(df, ["savings"], i="name", j="year", sep="_", suffix="\w+")
savings
name year
alex 2019A 10
bob 2019A 20
cathy 2019A 30
alex 2020A 40
bob 2020A 50
cathy 2020A 60
作为旁注,您还可以使用正则表达式 suffix=".*A" 来匹配以 "A" 结尾的所有列标签。
相关用法
- Python PySpark DataFrame withColumnRenamed方法用法及代码示例
- Python PySpark DataFrame withColumn方法用法及代码示例
- Python PySpark DataFrame where方法用法及代码示例
- Python Pandas DataFrame where方法用法及代码示例
- Python Pandas DataFrame empty属性用法及代码示例
- Python Pandas DataFrame pop方法用法及代码示例
- Python Pandas DataFrame nsmallest方法用法及代码示例
- Python Pandas DataFrame sample方法用法及代码示例
- Python Pandas DataFrame items方法用法及代码示例
- Python Pandas DataFrame max方法用法及代码示例
- Python Pandas DataFrame swaplevel方法用法及代码示例
- Python Pandas DataFrame agg方法用法及代码示例
- Python Pandas DataFrame copy方法用法及代码示例
- Python Pandas DataFrame pow方法用法及代码示例
- Python Pandas DataFrame insert方法用法及代码示例
- Python Pandas DataFrame lt方法用法及代码示例
- Python Pandas DataFrame all方法用法及代码示例
- Python Pandas DataFrame unstack方法用法及代码示例
- Python Pandas DataFrame mean方法用法及代码示例
- Python PySpark DataFrame filter方法用法及代码示例
- Python Pandas DataFrame tz_convert方法用法及代码示例
- Python Pandas DataFrame isin方法用法及代码示例
- Python PySpark DataFrame collect方法用法及代码示例
- Python PySpark DataFrame intersect方法用法及代码示例
- Python PySpark DataFrame dtypes属性用法及代码示例
注:本文由纯净天空筛选整理自Isshin Inada大神的英文原创作品 Pandas DataFrame | wide_to_long method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
