如何从pandas数据帧中列出列表,跳过nan值

时间:2017-07-31 20:25:23

标签: list pandas dataframe nested nan

我的pandas数据框大致类似于

    foo   foo2   foo3  foo4
a   NY    WA     AZ    NaN
b   DC    NaN    NaN   NaN
c   MA    CA     NaN   NaN

我想制作一个嵌套的数据框观察列表,但省略了NaN值,所以我有类似[[' NY',' WA&#39 ;,' AZ'],[' DC'],[' MA',CA&#39]。

此数据框中有一个模式,如果这有所不同,那么如果fooX为空,则后续列fooY也将为空。

我最初的代码如下所示。我确定有更好的方法来做到这一点

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

3 个答案:

答案 0 :(得分:3)

试试这个:

{{1}}

答案 1 :(得分:2)

选项1
pd.DataFrame.stack默认情况下会丢弃。

df.stack().groupby(level=0).apply(list).tolist()

[['NY', 'WA', 'AZ'], ['DC'], ['MA', 'CA']]

___

选项2
有趣的选择,因为我认为在pandas对象中汇总列表很有趣。

df.applymap(lambda x: [x] if pd.notnull(x) else []).sum(1).tolist()

[['NY', 'WA', 'AZ'], ['DC'], ['MA', 'CA']]

选项3
numpy实验

nn = df.notnull().values
sliced = df.values.ravel()[nn.ravel()]
splits = nn.sum(1)[:-1].cumsum()
[s.tolist() for s in np.split(sliced, splits)]

[['NY', 'WA', 'AZ'], ['DC'], ['MA', 'CA']]

答案 2 :(得分:0)

这是矢量化版本!

original = pd.DataFrame(data={
    'foo': ['NY', 'DC', 'MA'],
    'foo2': ['WA', np.nan, 'CA'],
    'foo3': ['AZ', np.nan, np.nan],
    'foo4': [np.nan] * 3,
})

out = original.copy().fillna('NAN')

# Build up mapping such that each non-nan entry is mapped to [entry]
#   and nan entries are mapped to []
unique_entries = np.unique(out.values)
mapping = {e: [e] for e in unique_entries}
mapping['NAN'] = []

# Apply mapping
for c in original.columns:
    out[c] = out[c].map(mapping)

# Concatenate the lists along axis 1
out.sum(axis=1)

您应该得到类似的东西

0    [NY, WA, AZ]
1            [DC]
2        [MA, CA]
dtype: object