Skip to content Skip to sidebar Skip to footer

Defining A Function For Changing Column Values And Creating New Datasets

I am trying to define a function where it will take a dataframe and change values in a column to create multiple new dataframes. As an example, from df1 looking like: df1: class

Solution 1:

Using np.identity (I changed your column name to class_ so it's not using a protected keyword):

arr = np.identity(len(df1))
arr[arr==0] = -1

dfs = [df1.assign(class_=arr[:, i]) for i in range(len(df1))]

for d in dfs:
    print(d, end='\n\n')

   class_ colB colC
01.01b   1c1-1.02b   2c2-1.03b   3c3-1.04b   4c4-1.05b   5c

   class_ colB colC
0-1.01b   1c11.02b   2c2-1.03b   3c3-1.04b   4c4-1.05b   5c

   class_ colB colC
0-1.01b   1c1-1.02b   2c21.03b   3c3-1.04b   4c4-1.05b   5c

   class_ colB colC
0-1.01b   1c1-1.02b   2c2-1.03b   3c31.04b   4c4-1.05b   5c

   class_ colB colC
0-1.01b   1c1-1.02b   2c2-1.03b   3c3-1.04b   4c41.05b   5c

Post a Comment for "Defining A Function For Changing Column Values And Creating New Datasets"