如何根据多列中的特定值将行值转换为列?

huangapple go评论72阅读模式
英文:

How to transform row values into column based on specific values from multiple columns?

问题

这是一个更大数据的示例。

我有一个类似这样的数据框:

df = pd.DataFrame({"Year":[2023, 2023, 2023, 2024, 2024, 2024],
                   "Value":[0, 2, 3, 1, 5, 2],
                   "Field":["A", "A", "B", "A", "B", "B"],
                   "ID":["X", "X", "X", "X", "Z", "Y"],
                   "Type":["class1","class2","class1","class1","class1","class2"]})

我想要基于df["Type"]的值创建新的列,并根据列Field和ID中的相同数据获取列df["Value"]中的值。因此,我的输出应该类似于这样:

   Year Field ID  class1  class2
0  2023     A  X     0.0     2.0
1  2023     B  X     3.0     NaN
2  2024     A  X     1.0     NaN
3  2024     B  Z     5.0     NaN
4  2024     B  Y     NaN     2.0

有人能帮助我吗?

英文:

This is an example of bigger data.

I have a dataframe like this:

df = pd.DataFrame({"Year":[2023, 2023, 2023, 2024, 2024, 2024],
                   "Value":[0, 2, 3, 1, 5, 2],
                   "Field":["A", "A", "B", "A", "B", "B"],
                   "ID":["X", "X", "X", "X", "Z", "Y"],
                   "Type":["class1","class2","class1","class1","class1","class2"]})

df
Out[8]: 
   Year  Value Field ID    Type
0  2023      0     A  X  class1
1  2023      2     A  X  class2
2  2023      3     B  X  class1
3  2024      1     A  X  class1
4  2024      5     B  Z  class1
5  2024      2     B  Y  class2

I would like to create new columns based on df["Type"] values and take the values from the column df["Value"] based on the same data in columns Field and ID. So my output should be something like this:

   Year Field ID  class1  class2
0  2023     A  X     0.0     2.0
1  2023     B  X     3.0     NaN
2  2024     A  X     1.0     NaN
3  2024     B  Z     5.0     NaN
4  2024     B  Y     NaN     2.0

Anyone could help me?

答案1

得分: 1

使用 [`df.pivot`][1] 函数

```python
res = df.pivot(columns='Type', values='Value', index=['Year', 'Field', 'ID']).reset_index()

   Type  Year Field ID  class1  class2
0  2023     A  X     0.0     2.0
1  2023     B  X     3.0     NaN
2  2024     A  X     1.0     NaN
3  2024     B  Y     NaN     2.0
4  2024     B  Z     5.0     NaN

<details>
<summary>英文:</summary>

Apply [`df.pivot`][1]:

    res = df.pivot(columns=&#39;Type&#39;, values=&#39;Value&#39;, index=[&#39;Year&#39;, &#39;Field&#39;, &#39;ID&#39;]).reset_index()


----------

    Type  Year Field ID  class1  class2
    0     2023     A  X     0.0     2.0
    1     2023     B  X     3.0     NaN
    2     2024     A  X     1.0     NaN
    3     2024     B  Y     NaN     2.0
    4     2024     B  Z     5.0     NaN


  [1]: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.pivot.html

</details>



huangapple
  • 本文由 发表于 2023年3月10日 01:38:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75688185.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定