将python数据框转换为列表 [英] convert python dataframe to list

查看:520
本文介绍了将python数据框转换为列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多列的Python dataFrame.

I have a Python dataFrame with multiple columns.

  LogBlk    Page                                    BayFail       
  0          0                                 [0, 1, 8, 9]  
  1          16           [0, 1, 4, 5, 6, 8, 9, 12, 13, 14]  
  2          32           [0, 1, 4, 5, 6, 8, 9, 12, 13, 14]  
  3          48           [0, 1, 4, 5, 6, 8, 9, 12, 13, 14]  

我想找到与LogBlk = 0和Page = 0关联的BayFails.

I want to find BayFails that is associated with LogBlk=0, and Page=0.

df2 = df[ (df['Page'] == 16) & (df['LogBlk'] == 0) ]['BayFail']

这将返回[0,1,8,9]

This will return [0,1,8,9]

我想要做的是将此pandas.series转换为列表.有人知道怎么做吗?

What I want to do is to convert this pandas.series into a list. Does anyone know how to do that?

推荐答案

pandas.Series,具有

pandas.Series, has a tolist method:

In [10]: import pandas as pd

In [11]: s = pd.Series([0,1,8,9], name = 'BayFail')

In [12]: s.tolist()
Out[12]: [0L, 1L, 8L, 9L]


技术说明:在我最初的回答中,我说Seriesnumpy.ndarray的子类,并继承了它的tolist方法.尽管这对于Pandas版本0.12或更早版本是正确的,但在即将发布的Pandas版本0.13中,Series已重构为NDFrame的子类. Series仍然具有tolist方法,但与同名的numpy.ndarray方法没有直接关系.


Technical note: In my original answer I said that Series was a subclass of numpy.ndarray and inherited its tolist method. While that's true for Pandas version 0.12 or older, In the soon-to-be-released Pandas version 0.13, Series has been refactored to be a subclass of NDFrame. Series still has a tolist method, but it has no direct relationship to the numpy.ndarray method of the same name.

这篇关于将python数据框转换为列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆