Home > Mobile >  How to covert dataframe to list without none values
How to covert dataframe to list without none values

Time:11-16

How can I remove the None values from this dataframe df and convert the columns from a to f as a list

    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

The output should be:

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]]

CodePudding user response:

This isn't really a panda's problem, here with just list comprehension:

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

Output:

[[[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]]]

CodePudding user response:

Pandas

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

CodePudding user response:

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