Home > database >  How to split dataframe based on row values of a column and make array with the divided groups as col
How to split dataframe based on row values of a column and make array with the divided groups as col

Time:12-16

I have a dataframe with four columns. In the column 'Regime' there are 2 groups (Ctrl and FR). I would like to create an array where the rows are the replications, and the columns the values based on the ligt regime. divide the column Value based on the column Regime and make two new columns: Value_Ctrl and Value_FR.

This is how the original dataframe looks like:

Replication   Regime     Value
 1          Ctrl          2
 2          Ctrl          7
 3          Ctrl          1
 4          Ctrl          5
 1          FR           12
 2          FR           22
 3          FR           52
 4          FR           22

I would like to have

data = np.array[2, 12
                7, 22
                1, 52, 
                5, 22]


                               

CodePudding user response:

Use DataFrame.pivot with DataFrame.to_numpy:

a = df.pivot('Replication','Regime','Value').to_numpy()
print (a)
[[ 2 12]
 [ 7 22]
 [ 1 52]
 [ 5 22]]

CodePudding user response:

Try this please:

df.groupby('Replication').agg({'Value': list}).values

CodePudding user response:

df1 = df[df[1] == 'CTRL']

df2 = df[df[1] == 'FR']

m = pd.merge(df1,df2,on=0)

Then delete the unnecessary columns

  • Related