1

I have two dataframes:

df1 = pd.DataFrame(rng.rand(1000, 3), columns=['A', 'B', 'C'])
df2 = pd.DataFrame(rng.rand(1000, 3), columns=['A', 'B', 'C'])

I also have a column consisting of "Y" and "N":

df0['Split'] = ['Y', 'N', 'Y'...]

I want to create a 3rd dataframe that returns df1 if df0['Split'] = 'Y' and returns df2 if df0['Split'] = 'N'. I'd like to keep the shape of the original two dataframes if possible.

I thought I could do something such as the following:

if df0['Split'] == Y:
    return df1
if df0['Split'] == N:
    return df2
else:
    return 0

In reality, I have a lot more columns than A through C. Appreciate your help.

1 Answer 1

2

You can use Boolean filtering to construct two dataframes, combine via pd.concat, then sort_index. The solution assumes all 3 dataframes share the same index.

mask = df0['Split'] == 'Y'
res = pd.concat([df1[mask], df2[~mask]]).sort_index()
Sign up to request clarification or add additional context in comments.

Comments

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.