2017-05-31 48 views
4

我有一個看起來像這樣的數據:大熊貓或Python相當於tidyr完整

library("tidyverse") 

df <- tibble(user = c(1, 1, 2, 3, 3, 3), x = c("a", "b", "a", "a", "c", "d"), y = 1) 
df 

# user  x  y 
# 1  1  a  1 
# 2  1  b  1 
# 3  2  a  1 
# 4  3  a  1 
# 5  3  c  1 
# 6  3  d  1 

Python的格式:

import pandas as pd 
df = pd.DataFrame({'user':[1, 1, 2, 3, 3, 3], 'x':['a', 'b', 'a', 'a', 'c', 'd'], 'y':1}) 

我想「完整」的數據幀,這樣每user有每個可能的x的記錄,默認y填充設置爲0.

這在R(tidyverse/tidyr)中有些微不足道:

df %>% 
    complete(nesting(user), x = c("a", "b", "c", "d"), fill = list(y = 0)) 

# user  x  y 
# 1  1  a  1 
# 2  1  b  1 
# 3  1  c  0 
# 4  1  d  0 
# 5  2  a  1 
# 6  2  b  0 
# 7  2  c  0 
# 8  2  d  0 
# 9  3  a  1 
# 10 3  b  0 
# 11 3  c  1 
# 12 3  d  1 

在pandas/python中是否有等效的complete會產生相同的結果?

回答

4

您可以通過MultiIndex.from_product使用reindex

df = df.set_index(['user','x']) 
mux = pd.MultiIndex.from_product([df.index.levels[0], df.index.levels[1]],names=['user','x']) 
df = df.reindex(mux, fill_value=0).reset_index() 
print (df) 
    user x y 
0  1 a 1 
1  1 b 1 
2  1 c 0 
3  1 d 0 
4  2 a 1 
5  2 b 0 
6  2 c 0 
7  2 d 0 
8  3 a 1 
9  3 b 0 
10  3 c 1 
11  3 d 1 

或者set_index + stack + unstack

df = df.set_index(['user','x'])['y'].unstack(fill_value=0).stack().reset_index(name='y') 
print (df) 
    user x y 
0  1 a 1 
1  1 b 1 
2  1 c 0 
3  1 d 0 
4  2 a 1 
5  2 b 0 
6  2 c 0 
7  2 d 0 
8  3 a 1 
9  3 b 0 
10  3 c 1 
11  3 d 1 
+1

你咬碎了這一點在3分鐘內?虛幻 – emehex