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

Python – Pandas, get a column, modify, re insert into dataframe as new column?

I have a dataframe and I am pulling out a specific column with the index. I want to perform a split on that column and get the [1] value.

The column looks like;

Name
t_alpaha_omega
t_bravo_omega
d_charlie_omega
t_delta_omega

I need to split on _ and get alpha, bravo, charlie, delta. Then add those values as a new column in my dataframe.

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

I am getting the name column like;


final_df.loc[:,"Name"]

I can do the splitting, I just don’t know how to insert the data as a new column.

Thanks.

I am plying with this and seeing if I can use a variation of it.

final_df.insert(1, "Test", final_df.loc[:,"Name"], True)

>Solution :

For this purpose we could use pd.Series.str.extract and define a named capturing group like (?P<New_Column_Name>...) as follows:

df['Value'] = df.Name.str.extract('(?P<Value>(?<=_)\w+(?=_))')
df

              Name    Value
0   t_alpaha_omega   alpaha
1    t_bravo_omega    bravo
2  t_charlie_omega  charlie
3    t_delta_omega    delta

If you would like to change the position of the column. We could first extract it with pd.Series.pop and the we insert it at the column index we would like:

new_column = df.pop('Value')
df.insert(0, 'Value', new_column)

     Value             Name
0   alpaha   t_alpaha_omega
1    bravo    t_bravo_omega
2  charlie  t_charlie_omega
3    delta    t_delta_omega
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