Home > database >  How to create two dataframes from a given dataframe?
How to create two dataframes from a given dataframe?

Time:03-24

Assume I have the following data frame:

enter image description here

I want to create two data frames such that for any row if column Actual is equal to column Predicted then the value in both columns goes in one data frame otherwise both columns go in another data frame.

For example, row 0,1,2 goes in dataframe named correct_df and row 245,247 goes in dataframe named incorect_df

CodePudding user response:

Use boolean indexing:

m = df['Actual'] == df['Predicted']
correct_df = df.loc[m]
incorrect_df = df.loc[~m]

CodePudding user response:

You can use this :

df_cor = df.loc[(df['Actual'] == df['Predicted'])]    
df_incor = df2 = df.loc[(df['Actual']!= df['Predicted'])]

And use reset_index if you want a new index.

  • Related