'How to create two dataframes from a given dataframe?

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



Solution 1:[1]

Use boolean indexing:

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

Solution 2:[2]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Corralien
Solution 2 desertnaut