python pandas将数据框展开列表 [英] python pandas flatten a dataframe to a list

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

问题描述

我有一个这样的df:

import pandas
a=[['1/2/2014', 'a', '6', 'z1'], 
   ['1/2/2014', 'a', '3', 'z1'], 
   ['1/3/2014', 'c', '1', 'x3'],
   ]
df = pandas.DataFrame.from_records(a[1:],columns=a[0])

我想平铺df,因此它是一个连续的列表:

I want to flatten the df so it is one continuous list like so:

['1/2/2014','a','6','z1','1/2/2014','a','3','z1 ','1/3/2014','c','1','x3']

扩展到列表,但是更容易做到这一点?

I can loop through the rows and extend to a list, but is a much easier way to do it?

推荐答案

你可以做:

df.values.flatten()

,如果您希望结果为Python,您还可以添加 .tolist() 列表

and you can also add .tolist() if you want the result to be a Python list.

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

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