Home > Software engineering >  Get a column, modify, re insert into dataframe as new column?
Get a column, modify, re insert into dataframe as new column?

Time:11-20

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.

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.

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)

CodePudding user response:

Hope the below code helps.

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

It works perfectly for a string though.

sample output

CodePudding user response:

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 (?=_))')
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
  • Related