1

I have a function I use to make duplicate rows in Pandas in order to have in each row a different value that were together before

GRI_Code     ADX_Code
  102        S1.2\nS1.1\nS5.1\nS5.2
  102\n405   S4.2\nS4.3\nS4.1
  103        E7.2\nE7.3\nE7.1\n\n\nS9.2\nS9.1\nS10.2\nS10.1\n
  302        E3.1\nE3.2\n\n

the method I use is:

def separate_code(self, df, column, delimiter):
    df = df.assign(GRI_Code=df[f'{column}'].str.split(delimiter)).explode(f'{column}')
    
    return df

When I call the funtion:

df = separate_code(df, column='GRI_Code', delimiter="\n")

My output is:

GRI_Code    ADX_Code
   102      \n\nS1.2\nS1.1\nS5.1\nS5.2
   102      S4.2\nS4.3\nS4.1 
   405      S4.2\nS4.3\nS4.1
   103      E7.2\nE7.3\nE7.1\n\n\nS9.2\nS9.1\nS10.2\nS10.1\n
   302      E3.1\nE3.2\n\n

I will be using this method on other dataframes with different column names, I would like to know how can I add the column= in a dynamic way, if I use the variable column instead of GRI_Code= it is going to create a new column and return the same values in the same row as a list, I don't want that.

1 Answer 1

1

Use a dictionary and parameter expansion:

def separate_code(self, df, column, delimiter):
    return df.assign(**{column: df[f'{column}']...})
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, it works, it separates the values that are together, but on the rows where the new separated values are, I don't get the same other values from previous rows.
Well, I didn't check anything about the logic, I just answered the primary question: "I would like to know how can I add the column= in a dynamic way" ;)
Sir mozway is there any other way than this df1.isnull().values.any() for checking null values in a dataframe in single value?
seems like a good approach to me
Why? What is wrong with your way?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.