[英]Reshaping/stacking 2 columns using pandas?
I'm relatively new to Python/Pandas.我对 Python/Pandas 比较陌生。 I have a list of products with sales and units by weeks like so:我有一个按周列出的销售和单位产品列表,如下所示:
Product(index) Desc1 Desc2 Desc3 Week1 Week2 Week3 Week1.1 Week2.1 Week3.1
Product1 Words1 Words1 Words1 $$$ $$$ $$$ ### ### ###
Product2 Words2 Words2 Words2 $$$ $$$ $$$ ### ### ###
Product3 Words3 Words3 Words3 $$$ $$$ $$$ ### ### ###
Desired result is:期望的结果是:
Product(index) Desc1 Desc2 Desc3 Sales (By Week) Units (By Week)
Product1 Words1 Words1 Words1 $$$ ###
Product1 Words1 Words1 Words1 $$$ ###
Product1 Words1 Words1 Words1 $$$ ###
.
.
.
ProductN WordsN WordsN WordsN $$$ ###
ProductN WordsN WordsN WordsN $$$ ###
ProductN WordsN WordsN WordsN $$$ ###
I tried doing something with multi-index:我尝试用多索引做一些事情:
iterables = [[Desc1, Desc2, Desc3],
[Week1, Week2, Week3, Week1.1, Week2.1, Week3.1]]
test1 = pd.DataFrame(iterables, names=['Sales', 'Units'])
pd.MultiIndex.from_frame(test1)
On a side note, the dates for both sales and units are the same (eg. Aug 1- 5) but when I read_csv
, it loaded the second column as .1
because of duplication.附带说明一下,销售日期和单位日期是相同的(例如 8 月 1 日至 5 日),但是当我read_csv
时,由于重复,它会将第二列加载为.1
。 Is this a problem?这是一个问题吗?
Let's try using pandas.melt
+ pandas.concat
让我们尝试使用pandas.melt
+ pandas.concat
import pandas as pd
sales = (
pd.melt(df, id_vars=['Product(index)', 'Desc1', 'Desc2', 'Desc3'],
value_vars=['Week1', 'Week2', 'Week3'],
value_name='Sales (By Week)')
)
unit = (
pd.melt(df, id_vars=['Product(index)', 'Desc1', 'Desc2', 'Desc3'],
value_vars=['Week1.1', 'Week2.1', 'Week3.1'],
value_name='Units (By Week)')
)
print(pd.concat([sales, unit[['Units (By Week)']]], axis=1))
Product(index) Desc1 Desc2 ... variable Sales (By Week) Units (By Week)
0 Product1 Words1 Words1 ... Week1 $$$ ###
1 Product2 Words2 Words2 ... Week1 $$$ ###
2 Product3 Words3 Words3 ... Week1 $$$ ###
3 Product1 Words1 Words1 ... Week2 $$$ ###
4 Product2 Words2 Words2 ... Week2 $$$ ###
5 Product3 Words3 Words3 ... Week2 $$$ ###
6 Product1 Words1 Words1 ... Week3 $$$ ###
7 Product2 Words2 Words2 ... Week3 $$$ ###
8 Product3 Words3 Words3 ... Week3 $$$ ###
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.