有时候会遇到一种情况,就是在dataframe的某一列中是比较复杂的数据结构,比如一个dict,或者一个list,或者一个list of list,其实都有很好的办法可以解决的,不需要用多重的for循环,这里放两个例子:
Splitting a list in a Pandas cell into multiple columns
源dataframe
df = pd.DataFrame(data=[[[8,10,12], 'A'],
[[7,9,11], 'B']])
目标效果:
0 1 2 3
0 8 10 12 A
1 7 9 11 B
其实很简单,只需要一行代码就能解决:
df[0].apply(pd.Series)
# 0 1 2
#0 8 10 12
#1 7 9 11
Update: To keep other columns of the data frame, you can concatenate the result with the columns you want to keep:
pd.concat([df[0].apply(pd.Series), df[1]], axis = 1)
# 0 1 2 1
#0 8 10 12 A
#1 7 9 11 B
另外这里还有其他的几个博客,可以用于参考:
How to convert lists to a dataframe
Convert a list of lists into a Pandas Dataframe
有一个很简单的函数:
nums =
[[1,2],
[3,4]]
a = sum(nums,[]) 一句话
除了转换成多列,还可以转换成行:
How to split a list inside a Dataframe cell into rows in Pandas
很好用吧
更新:
此外,如果在一列中出现的不是上述的list of list,而是dict,其实也很好解决,可以使用这一行代码
{'bids': [[1.0002, 612.52], [0.9998, 11.6], [0...
1 {'bids': [[11321.12, 0.005634999999999999], [1...
2 {'bids': [[0.46072, 1500], [0.46061, 1500], [0...
3 {'bids': [[11329.89, 0.0011], [11324.46, 0.069...
4 {'bids': [[0.027233, 0.606], [0.027232, 0.5660...
pd.DataFrame(list(meta_df['meta_data']))