Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit columns from list of tuples while dataframe creation

How to limit columns while creating df from list of tuples?

Expected Input:

data = [('a',11,111),
        ('b',22,222),
        ('c',33,333)]

Expected Output:

   A   B
0  a  11
1  b  22
2  c  33

I wrote:

pd.DataFrame([(x[0], x[1]) for x in data], columns=['A', 'B'])

BUT is there any more elegant way to do this?

like image 546
dtar Avatar asked Dec 17 '22 13:12

dtar


1 Answers

I think we can do from_records

pd.DataFrame.from_records(data,exclude=[2], columns=['A','B',2])
Out[27]: 
   A   B
0  a  11
1  b  22
2  c  33
like image 88
BENY Avatar answered Feb 23 '23 00:02

BENY