2016-06-28 52 views
2

的列表元素我有这样的DF:解压数据框中

l1 = ['a', 'b', 'c'] 
l2 = ['x', ['y1', 'y2', 'y3'], 'z'] 
df = pd.DataFrame(list(zip(l1, l2)), columns = ['l1', 'l2']) 

结果:

l1   l2 
0 a    x 
1 b [y1, y2, y3] 
2 c    z 

我需要的是解压在L2内部列表,并在这样的L1普及相应的值:

l1 l2 
0 a x 
1 b y1 
2 b y2 
3 b y3 
4 c z 

这样做的正确方法是什么? 谢谢。

回答

1

我想你可以通过嵌套listsstr.len和平板值由chain使用numpy.repeat由legths重复值:

from itertools import chain 

df1 = pd.DataFrame({ 
     "l1": np.repeat(df.l1.values, df.l2.str.len()), 
     "l2": list(chain.from_iterable(df.l2))}) 
print (df1) 
    l1 l2 
0 a x 
1 b y1 
2 b y2 
3 b y3 
4 c z 

时序

#[100000 rows x 2 columns] 
np.random.seed(10) 
N = 100000 
l1 = ['a', 'b', 'c'] 
l1 = np.random.choice(l1, N) 
l2 = [list(tuple(string.ascii_letters[:np.random.randint(1, 10)])) for _ in np.arange(N)] 
df = pd.DataFrame({"l1":l1, "l2":l2}) 
df.l2 = df.l2.apply(lambda x: x if len(x) !=1 else x[0]) 
#print (df) 


In [91]: %timeit (pd.DataFrame([(left, right) for outer in zip(l1, l2) for left, right in zip_longest(*outer, fillvalue=outer[0])])) 
1 loop, best of 3: 242 ms per loop 

In [92]: %timeit (pd.DataFrame({ "l1": np.repeat(df.l1.values, df.l2.str.len()), "l2": list(chain.from_iterable(df.l2))})) 
10 loops, best of 3: 84.6 ms per loop 

结论

numpy.repeat3 times更快,因为zip_longest解决方案在更大的df中。

编辑:

对于循环版本进行比较是necessery小DF,因为很慢:

#[1000 rows x 2 columns] 
np.random.seed(10) 
N = 1000 
l1 = ['a', 'b', 'c'] 
l1 = np.random.choice(l1, N) 
l2 = [list(tuple(string.ascii_letters[:np.random.randint(1, 10)])) for _ in np.arange(N)] 
df = pd.DataFrame({"l1":l1, "l2":l2}) 
df.l2 = df.l2.apply(lambda x: x if len(x) !=1 else x[0]) 
#print (df) 
def alexey(df): 
    df2 = pd.DataFrame(columns=df.columns,index=df.index)[0:0] 

    for idx in df.index: 
     new_row = df.loc[idx, :].copy() 
     for res in df.ix[idx, 'l2']: 
      new_row.set_value('l2', res) 
      df2.loc[len(df2)] = new_row 
    return df2 

print (alexey(df)) 

In [20]: %timeit (alexey(df)) 
1 loop, best of 3: 11.4 s per loop 

In [21]: %timeit pd.DataFrame([(left, right) for outer in zip(l1, l2) for left, right in zip_longest(*outer, fillvalue=outer[0])]) 
100 loops, best of 3: 2.57 ms per loop 

In [22]: %timeit pd.DataFrame({ "l1": np.repeat(df.l1.values, df.l2.str.len()), "l2": list(chain.from_iterable(df.l2))}) 
The slowest run took 4.42 times longer than the fastest. This could mean that an intermediate result is being cached. 
1000 loops, best of 3: 1.41 ms per loop 
+0

我可以让你权衡我的答案[** here **](http://stackoverflow.com/a/43020297/2336654)我回答迟了。我也在没有我的笔记本电脑的路上,不能运行任何代码 – piRSquared

+0

不幸的是我在电话上,所以无法测试。但我得到upvote。 – jezrael

+0

谢谢!我很感激。 – piRSquared

2

您可以使用嵌套列表理解itertools.zip_longest

import pandas as pd 

from itertools import zip_longest 

l1 = ['a', 'b', 'c'] 
l2 = ['x', ['y1', 'y2', 'y3'], 'z'] 

expanded = [(left, right) for outer in zip(l1, l2) 
          for left, right in zip_longest(*outer, fillvalue=outer[0])] 

pd.DataFrame(expanded) 

结果是...

0 1 
0 a x 
1 b y1 
2 b y2 
3 b y3 
4 c z 

对我来说这是对过长的列表比较的边界。还假设l1没有列表,并将进行填充。

1

蛮力,遍历数据框:

for idx in df.index: 
    # This transforms the item in "l2" into an iterable list 
    item = df.loc[idx, "l2"] if isinstance(df.loc[idx, "l2"], (list, tuple)) else [df.loc[idx, "l2"]] 
    for element in item: 
     print(df.loc[idx, "l1"], element) 

回报

a x 
b y1 
b y2 
b y3 
c z 
0

对于列不constatnt数DataFrames我现在做这样的事情:

l1 = ['a', 'b', 'c'] 
l2 = ['x', ['y1', 'y2', 'y3'], 'z'] 
df = pd.DataFrame(list(zip(l1, l2)), columns = ['l1', 'l2']) 

df2 = pd.DataFrame(columns=df.columns,index=df.index)[0:0] 

for idx in df.index: 
    new_row = df.loc[idx, :].copy() 
    for res in df.ix[idx, 'l2']: 
     new_row.set_value('l2', res) 
     df2.loc[len(df2)] = new_row 

它作品,但这看起来很像bruteforce。

+0

对我来说,没有工作,所以我不能将它添加到计时。 – jezrael

+0

但我认为它会很慢,因为循环:( – jezrael

+0

固定的代码,你可以请检查时间吗?是的,我猜它也很慢。也许循环可以优化某种方式(我不是专家) –