本文介绍了如何从Pandas DataFrame中创建列表列表,跳过Nan值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个看起来像大熊猫的数据框
I have a pandas dataframe that looks roughly like
foo foo2 foo3 foo4
a NY WA AZ NaN
b DC NaN NaN NaN
c MA CA NaN NaN
我想对此数据帧的观察结果进行嵌套列表,但省略NaN值,所以我有类似[['NY','WA','AZ'],['DC']的内容,['MA',CA'].
I'd like to make a nested list of the observations of this dataframe, but omit the NaN values, so I have something like [['NY','WA','AZ'],['DC'],['MA',CA'].
此数据帧中有一个模式,如果有区别,那么如果fooX为空,则后续列fooY也将为空.
There is a pattern in this dataframe, if that makes a difference, such that if fooX is empty, the subsequent column fooY will also be empty.
我最初在下面有类似以下代码的内容.我敢肯定有更好的方法可以做到这一点
I originally had something like this code below. I'm sure there's a nicer way to do this
A = [[i] for i in subset_label['label'].tolist()]
B = [i for i in subset_label['label2'].tolist()]
C = [i for i in subset_label['label3'].tolist()]
D = [i for i in subset_label['label4'].tolist()]
out_list = []
for index, row in subset_label.iterrows():
out_list.append([row.label, row.label2, row.label3, row.label4])
out_list
推荐答案
尝试一下:
In [77]: df.T.apply(lambda x: x.dropna().tolist()).tolist()
Out[77]: [['NY', 'WA', 'AZ'], ['DC'], ['MA', 'CA']]
这篇关于如何从Pandas DataFrame中创建列表列表,跳过Nan值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!