简体   繁体   中英

fill dataframe column with list of tuples

I have dataframe like below:

miles uid  
12    235   
13    234   
14    233   
15    236   

a list list1 like below:

[(39.14973, -77.20692),
 (33.27569, -86.35877),
 (42.55214, -83.18532),
 (41.3278, -95.96396)]

The output dataframe I want

miles uid lat-long               lat       long
12    235  (39.14973, -77.20692)  39.14973 -77.20692
13    234  (33.27569, -86.35877)  33.27569 -86.35877

How can I achieve this

you can use:

df['lat-long'] = my_list
df['lat'] = [e[0] for e in my_list]
df['long'] = [e[1] for e in my_list]

output:

在此处输入图像描述

If length of DataFrame is same like length of list first column only assign and for next 2 columns use list comprehension:

df['lat-long'] = list1
df['lat'] = [la for la, lo in list1]
df['lon'] = [lo for la, lo in list1]

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