# Break down into list of tuples
records = df.to_records().tolist()
# Convert tuples into lists
series = pd.Series(records).map(list)
# Get rid of empty strings
series.map(lambda row: list(filter(lambda x: x != '', row)))
# ... alternatively
series.map(lambda row: [x for x in row if x != ''])
导致
0 [0, 5, 9]
1 [1]
2 [2, 2, 1]
1
你可以用它
In[1]: [x[x.apply(lambda k: k != '')].tolist() for i, x in df.iterrows()]
Out[1]: [[5, 9], [], [2, 1]]
5 回答
可以这样做:
导致
你可以用它
您可以尝试使用df.values
只需要
df.values
. 将它们转换为列表并使用map
删除空元素:单线:
与@jezreal's solution相似 . 但是,如果您不希望
0
值,则可以使用空字符串的固有False
-ness:使用list comprehension删除空值:
或者通过参数
split
将to_dict的值转换为列表:然后删除空值: