简体   繁体   中英

How can I transform a list of tuples into a pandas dataframe so that the first value of each tuple represents a column?

I would like to transform my list of tuples so that the first element of each tuple represents 2 different columns. The second element of each tuple should represent the values that correspond to the columns in the pandas df.

My current list of tuples:


list_tuples = [('G', 9.8), ('B', 4.2), ('G', 9.6), ('B', 2.3), ('G',7.6), ('B', 3.1)]

Desired output:


            G        B   
           9.8      4.2      
           9.6      2.3      
           7.6      3.1      

The code I currently have which does not give desired output:


df = pd.DataFrame(list_tuples, columns=['G', 'B'])

Use defaultdict for convert list of tuples for dictionary of lists and then pass it to DataFrame constructor:

from collections import defaultdict

d = defaultdict(list)
for a, b in list_tuples:
    d[a].append(b)
df = pd.DataFrame(d)
print (df)
     G    B
0  9.8  4.2
1  9.6  2.3
2  7.6  3.1

Convert it to a dictionary, create your dataframe and clean it up with DataFrame.drop_duplicates and DataFrame.bfill :

list_tuples = [('G', 9.8), ('B', 4.2), ('G', 9.6), ('B', 2.3), ('G',7.6), ('B', 3.1)]

df = (pd.DataFrame([{col1:val} for col1, val in list_tuples])
        .bfill()
        .drop_duplicates('B')
        .reset_index(drop=True)
     )
     G    B
0 9.80 4.20
1 9.60 2.30
2 7.60 3.10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM