简体   繁体   中英

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

Use dropna first with specified column outlier for check NaN s:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM