如何在Python中部分转置CSV表

时间:2018-09-18 20:42:41

标签: python pandas

我正在读取一个csv文件,其中包含许多周按产品分类的每周销售数据,并试图部分转置数据,以便每一行代表按产品,商店,周进行的每周交易

从这里开始:

Product,Store,9/1/18,9/8/18,9/15/18,9/22/18
vacuum,123,1,5,3,3
toaster,456,5,7,4,10

为此:

Product,Store,Week,Sales
vacuum,123,9/1/18,1
vacuum,123,9/8/18,5
vacuum,123,9/15/18,3
vacuum,123,9/22/18,3
toaster,456,9/1/18,5
toaster,456,9/8/18,7
toaster,456,9/15/18,4
toaster,456,9/22/18,10...

我是Python的新手(已创建2天),我使用zip_longest / reader / writer半理解了完整的转置,但无法弄清楚如何制作部分版本

3 个答案:

答案 0 :(得分:4)

df.set_index(['Product','Store']).stack().reset_index()

输出:

   Product  Store  level_2   0
0   vacuum    123   9/1/18   1
1   vacuum    123   9/8/18   5
2   vacuum    123  9/15/18   3
3   vacuum    123  9/22/18   3
4  toaster    456   9/1/18   5
5  toaster    456   9/8/18   7
6  toaster    456  9/15/18   4
7  toaster    456  9/22/18  10

使用清理的列命名,

(df.set_index(['Product','Store'])
   .rename_axis('Week', axis=1)
   .stack()
   .rename('Sales')
   .reset_index())

输出:

   Product  Store     Week  Sales
0   vacuum    123   9/1/18      1
1   vacuum    123   9/8/18      5
2   vacuum    123  9/15/18      3
3   vacuum    123  9/22/18      3
4  toaster    456   9/1/18      5
5  toaster    456   9/8/18      7
6  toaster    456  9/15/18      4
7  toaster    456  9/22/18     10

答案 1 :(得分:3)

您可以使用melt来做到这一点:

df.melt(id_vars=['Product', 'Store',],
                       value_vars=['9/1/18', '9/8/18','9/15/18','9/22/18'], 
                       var_name='week', value_name='Sales')

    Product Store   week    Sales
0   vacuum  123  9/1/18     1
1   toaster 456  9/1/18     5
2   vacuum  123  9/8/18     5
3   toaster 456  9/8/18     7
4   vacuum  123  9/15/18    3
5   toaster 456  9/15/18    4
6   vacuum  123  9/22/18    3
7   toaster 456  9/22/18    10

答案 2 :(得分:1)

您可以使用df.pivot()

df.pivot(index='Product', columns='Store').stack(level=[1,0]).reset_index()

例如:

import pandas as pd

df = pd.read_csv('test.csv')

df = df.pivot(index='Product', columns='Store').stack(level=[1,0]).reset_index()

df.columns = ['Product','Store','Week','Sales']

哪个给:

   Product  Store     Week  Sales
0  toaster    456   9/1/18    5.0
1  toaster    456   9/8/18    7.0
2  toaster    456  9/15/18    4.0
3  toaster    456  9/22/18   10.0
4   vacuum    123   9/1/18    1.0
5   vacuum    123   9/8/18    5.0
6   vacuum    123  9/15/18    3.0
7   vacuum    123  9/22/18    3.0