Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

pandas how to split and update a dataframe column

I have dataframe df_student

     Name  Age           City    Stream  Percentage
0    Noah   22      London_UK      Math          90
1  Oliver   20  Birmingham_UK  Commerce          90
2  George   21     Glasgow_UK   Science          96
3  Arthur   19   Liverpool_UK      Math          75
4   Oscar   18     Bristol_UK      Math          70
5     Leo   22  Manchester_UK   Science          80

i want to split the df_student[‘City’] column by ‘_’ and update only the city name in the same column.

the resultant df_student dataframe should look like

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

     Name  Age           City    Stream  Percentage
0    Noah   22         London      Math          90
1  Oliver   20     Birmingham  Commerce          90
2  George   21        Glasgow   Science          96
3  Arthur   19      Liverpool      Math          75
4   Oscar   18        Bristol      Math          70
5     Leo   22     Manchester   Science          80

how can I achieve this. Please suggest.

>Solution :

You can do it using the following code.

df_student['City'] = df_student['City'].apply(lambda x: x.split('_')[0])

You can use the lambda function to implement it easily.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading