简体   繁体   English

将数据框转换为python中的列表列表

[英]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.使用此df.values.tolist()我可以将其转换为列表列表,如[['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]]...我把条件放在那里,但没有成功。

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]]但是当异常值不是NaN并且我想生成这样的输出时,我只想获取这些值.. [ ['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:首先使用dropna与指定的列outlier检查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 :您可以使用np.isnan创建掩码并过滤掉outlier值中的NaN值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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