Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python extract multiple values from a single cell in a dataframe column using pandas

#You have to explode your dataframe if year column contains list:

>>> df.explode('year')
  var1  year
0    a  2025
1    b  2025
1    b  2030
2    c  2023
2    c  2025
2    c  2030
2    c  2040

#If your column contains string representation of a list, you have to eval first:

>>> df.assign(year=pd.eval(df['year'])).explode('year')
  var1  year
0    a  2025
1    b  2025
1    b  2030
2    c  2023
2    c  2025
2    c  2030
2    c  2040
 
PREVIOUS NEXT
Tagged: #python #extract #multiple #values #single #cell #dataframe #column #pandas
ADD COMMENT
Topic
Name
8+3 =