Search
 
SCRIPT & CODE EXAMPLE
 

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
Comment

PREVIOUS NEXT
Code Example
Python :: find smallest element not present in list python 
Python :: print(i) 
Python :: dnpy notify 
Python :: how to create function python 
Python :: pixel accuracy image segmentation python 
Python :: groupby and add aggregated column 
Python :: seaborn colorbar labelsize 
Python :: Fetch all links from a given webpage 
Python :: object get in djangi 
Python :: how to show type of a variable 
Python :: Dynamic use of templates in Jinja2 
Python :: python tuple index access 
Python :: Python 3 (python 3.7.3) sample 
Python :: Like strings (and all other built-in sequence type), lists can be indexed and sliced: 
Python :: perform cross tabulation sklearn 
Python :: ring Date and Time Clock 
Python :: qtextedit insert unicode 
Python :: loop over dict python looking for match in list 
Python :: StandardScaler sklearn get params normalization 
Python :: SimpleITK interpolation 
Python :: python list of datetimes as type string 
Python :: custom 3d image generator for segmentation 
Python :: how to hash out a big paragraph in vs code python 
Python :: pylatex multicolumn align 
Python :: login system user exist in textfile python 
Python :: count items in a model django rest 
Python :: seaborn heatmap spearman correlation coefficient 
Python :: python token stealer 
Python :: python autotrader web 
Python :: tkinter auto refresh content periodically 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =