Search code examples
pythonpandaslistextractelement

How do I access an element in a list within a column?


My goal is to extract elements from a list within a column to new columns.

enter image description here

I have tried using the common bracket notation:

df['lat'] = df['query'][0]
 
df['lon'] = df['query'][1]

The error I get is: "ValueError: Length of values (2) does not match length of index (1)"

What is the error trying to say?


Solution

  • Here is a solution you can try out,

    df['lat'], df['lon'] = df['query'].str[0], df['query'].str[1]