[英]How can I separate a column in new columns
I don't know how to ask this question, but i'll try do explain my case.我不知道如何问这个问题,但我会尽力解释我的情况。
I have a dataset with the data as following:我有一个包含以下数据的数据集:
Product![]() |
Value![]() |
Value type![]() |
year![]() |
---|---|---|---|
A![]() |
21,5 ![]() |
Price![]() |
21 ![]() |
A![]() |
5 ![]() |
Volume![]() |
21 ![]() |
B![]() |
55,3 ![]() |
Price![]() |
21 ![]() |
B![]() |
10 ![]() |
Volume![]() |
21 ![]() |
C ![]() |
70,0 ![]() |
Price![]() |
21 ![]() |
D ![]() |
37,5 ![]() |
Price![]() |
21 ![]() |
D ![]() |
7,7 ![]() |
Volume![]() |
21 ![]() |
And I want to reach something like that:我想达到这样的目标:
Product![]() |
Price![]() |
Volume![]() |
Year![]() |
---|---|---|---|
A![]() |
21,5 ![]() |
5 ![]() |
21 ![]() |
B![]() |
55,3 ![]() |
10 ![]() |
21 ![]() |
c ![]() |
70,0 ![]() |
- ![]() |
21 ![]() |
D ![]() |
37,0 ![]() |
7,7 ![]() |
21 ![]() |
I mind that the unstack function can solve the problem, but i don't know how, cause i'm not getting all the columns back.我认为 unstack 函数可以解决问题,但我不知道如何解决,因为我没有取回所有列。
I found a complex solution but it's not working.我找到了一个复杂的解决方案,但它不起作用。
container = []
for label, _df in df.groupby(['Year','Product']):
_df.set_index('Value type', inplace = True)
container.append(pd.DataFrame({
"Product": [label[1]],
"Price":[_df.loc['Price', 'Value']],
"Volume": [_df.loc['Volume', 'Value']],
"Year":[label[0]]}))
df_new = pd.concat(container)
This solution doesn't work, because the missing line for Volume for product C.此解决方案不起作用,因为产品 C 的 Volume 缺少行。
How can I reach the expected dataframe?我怎样才能达到预期的数据帧? Is there any fast way to calculate this?
有没有什么快速的方法来计算这个?
Use pivot
:使用
pivot
:
out = df.pivot(index=['Product', 'year'], columns='Value type', values=['Value']) \
.droplevel(0, axis=1).reset_index().rename_axis(None, axis=1) \
[['Product', 'Price', 'Volume', 'year']]
>>> out
Product Price Volume year
0 A 21.5 5.0 21
1 B 55.3 10.0 21
2 C 70.0 NaN 21
3 D 37.5 7.7 21
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.