【发布时间】:2022-01-14 19:13:30
【问题描述】:
我有以下要重塑的数据框:
year control Counts_annee_control
0 2014.0 0 81.0
1 2016.0 1 147.0
2 2016.0 0 168.0
3 2015.0 1 90.0
4 2016.0 1 147.0
我希望control 列成为新索引,因此我将只有两列。 year 值应该是数据框的新列,最后Counts_annee_control 列将填充数据框。
我尝试使用groupby 和transform,但我认为我做得不对。
【问题讨论】:
-
control不能作为索引,因为它包含重复值 -
你试过
df = df.set_index("control")吗?
标签: python pandas group-by transform reshape