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;


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

I am getting the name column like;


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

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

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



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

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

              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
Answered By: Anoushiravan R

Hope the below code helps.

newCol= [] 
for i in range(len(df)):
    a = df.iloc[i].to_list()
    requiredValue = a.split("_")[1]
df["newValue"] = requiredValue

It works perfectly for a string though.

sample output

Answered By: Harishma Ashok
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.