簡體   English   中英

熊貓數據透視表 - 重新排列

[英]pandas pivot table - rearrange

我有一些帶有一些列的pandas數據框。 我想以不同的方式重新排列它們。 一個例子如下:

time,name,feature,value
33 20 May 2016 14:00:00 -0700,John,badL,2
45 19 May 2016 18:00:00 -0700,John,badL,1
120 17 May 2016 11:00:00 -0700,John,badL,1
220 20 May 2016 14:00:00 -0700,John,totalL,20
450 19 May 2016 18:00:00 -0700,John,totalL,15
330 18 May 2016 15:00:00 -0700,Mary,badL,2
330 18 May 2016 15:00:00 -0700,Mary,totalL,20
550 21 May 2016 12:00:00 -0700,Mary,adCmd,4
700 22 May 2016 16:00:00 -0700,Mary,PC,3
800 22 May 2016 16:00:00 -0700,Mary,eCon,200

注意:第一列值(時間)前面是索引值(33,45,120,...)。 從上面的數據框中,我希望得到的數據框如下:

time,name,badL,totalL,adCmd,PC,eCon
20 May 2016 14:00:00 -0700,John,2,20,0,0,0
19 May 2016 18:00:00 -0700,John,1,15,0,0,0
17 May 2016 11:00:00 -0700,John,1,0,0,0,0
18 May 2016 15:00:00 -0700,Mary,2,20,0,0,0
21 May 2016 12:00:00 -0700,Mary,0,0,4,0,0
22 May 2016 16:00:00 -0700,Mary,0,0,0,3,200

注意:對於17日,約翰沒有任何總數。 所以,用0填充它。

有一種優雅的方式來做到這一點? 我將時間字段設置為pd.to_datetime,然后比較......看起來很乏味。 對於上面的例子,我只有兩個'功能'(badL,totalL)。 我稍后會再說幾句。

這就是我所擁有的 - 但是,它為第二個特征添加了不同的行...(totalL)....而不是將它放在同一行中。

for f in ['badL', 'totalL']:
    dff = df[df.feature == f]
    print dff
    if len(dff.index) > 0:
        fullFeatureDf[f] = dff.feature_value

設定

from StringIO import StringIO
import pandas as pd

text = '''time,name,f1,value
20 May 2016 14:00:00 -0700,John,badL,2
19 May 2016 18:00:00 -0700,John,badL,1
17 May 2016 11:00:00 -0700,John,badL,1
20 May 2016 14:00:00 -0700,John,totalL,20
19 May 2016 18:00:00 -0700,John,totalL,15
17 May 2016 11:00:00 -0700,John,totalL,12
'''

df = pd.read_csv(StringIO(text))

print df

                         time  name      f1  value
0  20 May 2016 14:00:00 -0700  John    badL      2
1  19 May 2016 18:00:00 -0700  John    badL      1
2  17 May 2016 11:00:00 -0700  John    badL      1
3  20 May 2016 14:00:00 -0700  John  totalL     20
4  19 May 2016 18:00:00 -0700  John  totalL     15
5  17 May 2016 11:00:00 -0700  John  totalL     12

解決方案使用unstack

df = df.set_index(['time', 'name', 'f1'])

print df

                                        value
time                       name f1           
20 May 2016 14:00:00 -0700 John badL        2
19 May 2016 18:00:00 -0700 John badL        1
17 May 2016 11:00:00 -0700 John badL        1
20 May 2016 14:00:00 -0700 John totalL     20
19 May 2016 18:00:00 -0700 John totalL     15
17 May 2016 11:00:00 -0700 John totalL     12

然后取消堆棧以執行樞軸。 它占用行索引的一部分並將其移動為列。

print df.unstack()

                                value       
f1                               badL totalL
time                       name             
17 May 2016 11:00:00 -0700 John     1     12
19 May 2016 18:00:00 -0700 John     1     15
20 May 2016 14:00:00 -0700 John     2     20

在精神上,這是與Yakym Pirozhenko完全相同的解決方案。 這樣做的方式略有不同。 這對我來說更直觀,但可能不適合你。

這是df.pivot的工作:

import pandas as pd
from io import StringIO

df = pd.read_csv(StringIO(
'''
time,name,feature,value
33 20 May 2016 14:00:00 -0700,John,badL,2
45 19 May 2016 18:00:00 -0700,John,badL,1
120 17 May 2016 11:00:00 -0700,John,badL,1
220 20 May 2016 14:00:00 -0700,John,totalL,20
450 19 May 2016 18:00:00 -0700,John,totalL,15
330 18 May 2016 15:00:00 -0700,Mary,badL,2
330 18 May 2016 15:00:00 -0700,Mary,totalL,20
550 21 May 2016 12:00:00 -0700,Mary,adCmd,4
700 22 May 2016 16:00:00 -0700,Mary,PC,3
800 22 May 2016 16:00:00 -0700,Mary,eCon,200
'''), sep=',').set_index(['time', 'name'])

df_new = df.pivot(columns='feature').fillna(0).astype(int)

#                                     value
# feature                                PC adCmd badL eCon totalL
# time                           name
# 120 17 May 2016 11:00:00 -0700 John     0     0    1    0      0
# 220 20 May 2016 14:00:00 -0700 John     0     0    0    0     20
# 33 20 May 2016 14:00:00 -0700  John     0     0    2    0      0
# 330 18 May 2016 15:00:00 -0700 Mary     0     0    2    0     20
# 45 19 May 2016 18:00:00 -0700  John     0     0    1    0      0
# 450 19 May 2016 18:00:00 -0700 John     0     0    0    0     15
# 550 21 May 2016 12:00:00 -0700 Mary     0     4    0    0      0
# 700 22 May 2016 16:00:00 -0700 Mary     3     0    0    0      0
# 800 22 May 2016 16:00:00 -0700 Mary     0     0    0  200      0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM