简体   繁体   English

为了进行这种转换,我可以在 Pandas dataframe 中使用哪个 Function?

[英]Which Function can I use in Pandas dataframe in order to have this transformation?

I am beginning to use pandas and need to know how to do the transformation that follows:我开始使用pandas并且需要知道如何进行以下转换:

Initial dataset:初始数据集:

在此处输入图像描述

Desired dataset:所需数据集:

在此处输入图像描述

I've tried to use df.pivot and pd.pivot_table , but I can't have the same result.我尝试使用df.pivotpd.pivot_table ,但我不能得到相同的结果。 I know how to this in Excel, but I'm trying to learn by this method too.我知道如何在 Excel 中做到这一点,但我也在尝试通过这种方法学习。 I could not understand the examples on the documentation.我无法理解文档中的示例。

I would appreciate some help here!我会很感激这里的一些帮助!

The transformation you are looking for is pd.melt() ( documentation ):您正在寻找的转换是pd.melt()文档):

  • id_vars : tuple, list, or ndarray, optional id_vars元组、列表或 ndarray,可选
    Column(s) to use as identifier variables.用作标识符变量的列。

  • value_vars tuple, list, or ndarray, optional value_vars元组、列表或 ndarray,可选
    Column(s) to unpivot.要取消透视的列。 If not specified, uses all columns that are not set as id_vars.如果未指定,则使用所有未设置为 id_vars 的列。

  • var_name scalar var_name标量
    Name to use for the 'variable' column.用于“变量”列的名称。 If None it uses frame.columns.name or 'variable'.如果 None 它使用 frame.columns.name 或“变量”。

  • value_name scalar, default 'value' value_name标量,默认“值”
    Name to use for the 'value' column.用于“值”列的名称。

df = pd.melt(df,
             id_vars = ['Regions'],
             value_vars = ['1°', '2°', '3°', '4°'],
             var_name = 'Label',
             value_name = 'Value')

Initial dataframe:初始 dataframe:

                             Regions       1°     2°     3°     4°
0                             Africa    23.31  23.41  23.86  23.95
1                               Asia    18.95  19.21  20.35  20.42
2            Australia/South Pacific    17.01  17.33  17.11  17.37
3                             Europe    10.43  10.11  11.33  11.64
4                        Middle East    21.99  22.78  24.16  23.90
5                      North America    12.66  12.10  14.27  13.81
6  South/Central America & Carribean    22.13  22.39  23.00  22.88

Transformed dataframe:变形 dataframe:

                              Regions Label  Value
0                              Africa    1°  23.31
1                                Asia    1°  18.95
2             Australia/South Pacific    1°  17.01
3                              Europe    1°  10.43
4                         Middle East    1°  21.99
5                       North America    1°  12.66
6   South/Central America & Carribean    1°  22.13
7                              Africa    2°  23.41
8                                Asia    2°  19.21
9             Australia/South Pacific    2°  17.33
10                             Europe    2°  10.11
11                        Middle East    2°  22.78
12                      North America    2°  12.10
13  South/Central America & Carribean    2°  22.39
14                             Africa    3°  23.86
15                               Asia    3°  20.35
16            Australia/South Pacific    3°  17.11
17                             Europe    3°  11.33
18                        Middle East    3°  24.16
19                      North America    3°  14.27
20  South/Central America & Carribean    3°  23.00
21                             Africa    4°  23.95
22                               Asia    4°  20.42
23            Australia/South Pacific    4°  17.37
24                             Europe    4°  11.64
25                        Middle East    4°  23.90
26                      North America    4°  13.81
27  South/Central America & Carribean    4°  22.88

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM