英文:
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='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
[1]: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.pivot.html
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论