[英]Convert list of tuples to dataframe - where first element of tuple is column name
I have a list of tuples in the format: 我有一个格式列表:
tuples = [('a',1,10,15),('b',11,0,3),('c',7,19,2)] # etc.
I wish to store the data in a DataFrame
with the format: 我希望将数据存储在DataFrame
,格式如下:
a b c ...
0 1 11 7 ...
1 10 0 19 ...
2 15 3 2 ...
Where the first element of the tuple is what I wish to be the column name. 元组的第一个元素是我希望的列名。
I understand that if I can achieve what I want by running: 我明白,如果我能通过运行实现我想要的东西:
df = pd.DataFrame(tuples)
df = df.T
df.columns = df.iloc[0]
df = df[1:]
But it seems to me like it should be more straightforward than this. 但在我看来,它应该比这更简单。 Is this a more pythonic way of solving this? 这是解决这个问题的更加pythonic的方法吗?
Here's one way 这是一种方式
In [151]: pd.DataFrame({x[0]:x[1:] for x in tuples})
Out[151]:
a b c
0 1 11 7
1 10 0 19
2 15 3 2
You can use dictionary comprehension, like: 您可以使用字典理解,例如:
pd.DataFrame({k:v for k,*v in tuples})
in python-3.x , or: 在python-3.x中 ,或者:
pd.DataFrame({t[0]: t[1:] for t in tuples})
in python-2.7 . 在python-2.7中 。
which generates: 产生:
>>> pd.DataFrame({k:v for k,*v in tuples})
a b c
0 1 11 7
1 10 0 19
2 15 3 2
The columns will be sorted alphabetically . 列将按字母顺序排序 。
If you want the columns to be sorted like the original content , you can use the columns
parameter: 如果希望对列进行排序,就像原始内容一样 ,可以使用columns
参数:
pd.DataFrame({k:v for k,*v in tuples},columns=[k for k,*_ in tuples])
again in python-3.x , or for python-2.7 : 再次在python-3.x或python-2.7中 :
pd.DataFrame({t[0]: t[1:] for t in tuples},columns=[t[0] for t in tuples])
We can shorten this a bit into: 我们可以将这一点缩短为:
from operator import itemgetter
pd.DataFrame({t[0]: t[1:] for t in tuples},columns=map(itemgetter(0),tuples))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.