I have a DataFrame I created using pandas and want to create new table based on the original, but filtered based on certain conditions.
df = pd.DataFrame(
[['Y', 'Cat', 'no', 'yes', 6],
['Y', 4, 7, 9, 'dog'],
['N', 6, 4, 6, 'pig'],
['N', 3, 6, 'beer', 8]],
columns = ('Data', 'a', 'b', 'c', 'd')
)
My condition that doesnt work:
if (df['Data']=='Y') & (df['Data']=='N'):
df3=df.loc[:,['Data', 'a', 'b', 'c']]
else:
df3=df.loc[:,['Data', 'a', 'b']]
I want the new table to contain data that matches the following criteria:
If df.Data has value 'Y' and 'N', the new table get columns ('Data', 'a', 'b')
If not, the new table gets columns ('Data', 'a', 'b', 'c')
Data a b
0 Y Cat no
1 Y 4 7
2 N 6 4
3 N 3 6
Data a b c
0 Y Cat no yes
1 Y 4 7 9
2 Y 6 4 6
3 Y 3 6 beer
YandNbut no columncwhile second output has onlyYbut it has columnc. That differs from your verbal description.