简体   繁体   English

将元组列表转换为dataframe - 其中元组的第一个元素是列名

[英]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 , or: ,或者:

pd.DataFrame({t[0]: t[1:] for t in tuples})

in .

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 , or for : 再次在

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.

相关问题 如何将列表列表转换为 dataframe 其中第一个元素是索引,第二个是列名 - How to convert a list of lists into a dataframe where first element is index, second is column name 检查元组的列表,其中元组的第一个元素由定义的字符串指定 - Check list of tuples where first element of tuple is specified by defined string 将包含字典列表的列的 pandas dataframe 转换为元组的元组 - Convert pandas dataframe with column containing list of dictionaries to tuple of tuples 元组列表中元组的小写第一个元素 - Lowercase first element of tuple in list of tuples 访问元组列表中元组第一个元素的范围 - Accessing a range of the first element of a tuple in a list of tuples 将元组列表转换为数组,其中每个元组的第一个元素是数组索引? - Convert lists of tuples to an array, where the first element in each tuple is the array index? 创建元组列表的列表,其中每个元组是第一个出现的字母及其列表中的行和列 - Create a list of lists of tuples where each tuple is the first occurrence of a letter along with its row and column in the list of lists 如何将元组列表转换为 Pandas 数据框,以便每个元组的第一个值代表一列? - How can I transform a list of tuples into a pandas dataframe so that the first value of each tuple represents a column? 如何从列表创建单个列的DataFrame,其中第一个元素是python中的列名 - How to create a DataFrame of a single column from a list where the first element is the column name in python 如何将元组列表写入到csv文件中,其中元组的第一个元素是字典? - How can I write a list of tuples to a csv file where the first element of the tuple is a dictionary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM