pandas 如何将 Dataframe 转换为不带none值的列表

ia2d9nvy  于 2022-11-20  发布在  其他
关注(0)|答案(3)|浏览(205)

如何从该 Dataframe df中删除None值,并将列从a转换为f作为列表

emp_no     a             b           c           d          e               f         id
0   11390   [1, 28.4]   [7, 32.2]   [7, 31.3]   [28, 40.7]  [28, 40.0]  [28, 39.6]    nhvm657mjhgmjhm
1   11395   [1, 31.4]   [7, 32.8]   [7, None]   [28, 37.3]  [28, 39.2]  [28, None]    hjgj6hgjgghjjh
2   11397   [1, 33.0]   [7, 33.1]   [7, 31.5]   [28, None]  [28, 40.4]  [28, 41.0]    fjyj676hjhjhg
3   11522   [1, 34.0]   [7, 32.4]   [7, 32.6]   [28, 45.4]  [28, 45.9]  [28, 46.9]    65hjhgjghj766
4   11525   [1, 32.7]   [7, 31.9]   [7, 32.0]   [28, None]  [28, 44.4]  [28, 46.1]    ftghjy6757hjh

输出应为:

df[0] = [[1, 28.4],[7, 32.2],[7, 31.3],[28, 40.7],[28, 40.0],[28, 39.6]]
df[1] = [[1, 31.4],[7, 32.8],[28, 37.3],[28, 39.2]] 
df[2] = [[1, 33.0],[7, 33.1],[7, 31.5],[28, 40.4],[28, 41.0]]
df[3] = [[1, 34.0],[7, 32.4],[7, 32.6],[28, 45.4],[28, 45.9],[28, 46.9]]
df[4] = [[1, 32.7]  [7, 31.9],[7, 32.0],[28, 44.4],[28, 46.1]]
v7pvogib

v7pvogib1#

这其实不是Pandas的问题,这里只需要列表理解:

out = [[y for y in x if None not in y] for x in df.iloc[:, 1:].to_dict('list').values()]
print(out)

输出量:

[[[1, 28.4], [7, 32.2], [7, 31.3], [28, 40.7], [28, 40.0], [28, 39.6]],
 [[1, 31.4], [7, 32.8], [28, 37.3], [28, 39.2]],
 [[1, 33.0], [7, 33.1], [7, 31.5], [28, 40.4], [28, 41.0]],
 [[1, 34.0], [7, 32.4], [7, 32.6], [28, 45.4], [28, 45.9], [28, 46.9]],
 [[1, 32.7], [7, 31.9], [7, 32.0], [28, 44.4], [28, 46.1]]]
cgfeq70w

cgfeq70w2#

Pandas

data = []
for idx, r in df.iterrows():
    row = []
    for col in r:
        if None not in col:
            row.append(col)
    data.append(row)
wrrgggsh

wrrgggsh3#

out = [[y for y in x if None not in y] for x in df.iloc[:,1:6].values.tolist()]
    
out

相关问题