user11022126
user11022126

Reputation:

convert data-frame into lists of list in python

I have a data-frame like this

    timeslice             host  CPU  outlier
0  2011-01-10 19:28:31     1   56      NaN
1  2012-02-10 18:28:31     2   78      NaN
2  2013-03-10 12:28:31     3    3      3.0
3  2014-04-10 14:28:31     4   98      NaN
4  2015-04-10 14:28:31     7   72      NaN
5  2014-06-10 14:28:31     6    7      7.0
6  2018-04-10 14:28:31     4    9      9.0

using this df.values.tolist() i can convert this to lists of list like [['2011-01-10 19:28:31', 1, 56, nan], ['2012-02-10 18:28:31', 2, 78, nan], ['2013-03-10 12:28:31', 3, 3, 3.0], ['2014-04-10 14:28:31', 4, 98, nan]]... i put condition there but it didn't work out.

but I want to fetch only those values when outlier is not NaN and i want to generate a output like this.. [ ['2013-03-10 12:28:31', 3, 3, 3.0],[2014-06-10 14:28:31,6,7,7.0],[2018-04-10 14:28:31 ,4 ,9 ,9.0]]

Thanks in Advance

Upvotes: 1

Views: 112

Answers (2)

Dani Mesejo
Dani Mesejo

Reputation: 61910

You could use np.isnan to create a mask and filter out the NaN values in outlier:

result = df[~np.isnan(df.outlier)].values.tolist()
print(result)

Output

[['12:28:31', 3, 3, 3.0], ['14:28:31', 6, 7, 7.0], ['14:28:31', 4, 9, 9.0]]

Upvotes: 0

jezrael
jezrael

Reputation: 863146

Use dropna first with specified column outlier for check NaNs:

L = df.dropna(subset=['outlier']).values.tolist()
print (L)
[['12:28:31', 3, 3, 3.0], ['14:28:31', 6, 7, 7.0], ['14:28:31', 4, 9, 9.0]]

Upvotes: 1

Related Questions