1

我有以下字典:

d = {'col1': ['a', 'b', 'c'],
     'col2': [[1,2], [4,3,2], []],
}

我想要一个 Pandas DataFrame,例如:

idx, col1, col2
  0,  'a',  1
  1,  'a',  2
  2,  'b',  4
  3,  'b',  3
  4,  'b',  2
  5   'c',  nan

如何构建?如果我只是通过 dict,它不会解开/重复 col2 中的列表项。谢谢!

4

1 回答 1

3

你只需要自己构建它。这是一种方法:

col1 = ['a', 'b', 'c']
col2 = [[1,2], [4,3,2], []]
col2_lens = map(len, col2)

# flatten col2
s2 = Series([eli for el in col2 for eli in (el or [nan])])

# replicate elements of col1 col2[i] times
s1 = Series(list(''.join(el * (col2_len or 1) for el, col2_len in zip(col1, col2_lens))))
concat([s1, s2], axis=1)

屈服

   0   1
0  a   1
1  a   2
2  b   4
3  b   3
4  b   2
5  c NaN

这是%%timeit此处显示的 3 种方法的 s

1.

%%timeit
col2_lens = map(len, col2)

# flatten col2
s2 = Series([eli for el in col2 for eli in (el or [nan])])

# replicate elements of col1 col2[i] times
s1 = Series(list(''.join(el * (col2_len or 1) for el, col2_len in zip(col1, col2_lens))))
concat([s1, s2], axis=1)

1000 loops, best of 3: 646 µs per loop

2.

%%timeit
df = DataFrame()
for a, b in zip(col1, col2):
    df = pd.concat([df, pd.DataFrame({'col1': a, 'col2': b or [np.nan]})])

100 loops, best of 3: 2.52 ms per loop

3.

%%timeit
frames = []
for a, b in zip(col1, col2):
    frames.append(pd.DataFrame({'col1': a, 'col2': b or [np.nan]}))
df = pd.concat(frames)

1000 loops, best of 3: 1.58 ms per loop
于 2013-08-16T21:50:43.513 回答