python - Pivoting of dataframe by year and month
问题描述
I have a data frame like this:
Factory Serial_number year month LT Order Minimum Lot Stock Value
xxx 123 2020 8 20 123 456 10 2000 3000
xxx 123 2020 9 .. ... ... .. ... 4000
Desired output is:
Factory Serial_number year month LT Order Minimum Lot Stock 8.2020 9.2020
xxx 123 2020 8 20 123 456 10 2000 3000 4000
I tried different method like stacking, pivoting but that did not give the exact result
pd.pivot_table(df, values='Value', index=['Factory', 'Serial_number','Order','Minimum Lot','Stock'],
columns=['year','month']).reset_index()
Ouput I get:
year 2020
month 8 9 10 11 12 1 2 3 4 5 6 7
Factory 12NC Order Minimum Lot Stock
xxx 1233 8430.0 305.0 10675.200 35295.86 38735.9 29546.91 23004.1 25433.2 0.0 13547.6 27095.2 0.0 2686 0.0 0.0 0.0
1234 660.0 330.0 108.000 0.00 0.0 0.00 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
解决方案
推荐阅读
- c# - 适用于所有 Android 移动设备的所有纵横比的 Unity 缩放
- hibernate-mapping - 在 Hibernate 中映射 2 个具有相同结构的表
- python - 使用 sqlalchemy `session` 对象进行批量插入和更新
- cucumber - 如何在testng runner类的特征标签中一一执行特征文件中提到的顺序
- mysql - 在子查询mysql中使用父查询的id列
- xmpp - 无法在 ejabberd-18-06 上处理自定义 iq
- android - 用于在 Android 应用中调试和发布的分析属性
- react-native - 在本机反应中选择时如何更改列表项背景颜色?
- android - 为什么“doOnError”在“err->{}”没有传递给“subscribe()”方法之前不起作用
- algorithm - 我如何理解hackerrank中一个问题的具体解决方案?