I have a pretty basic problem as I try to get deeper into Python. Basically, I am making a dataframe using pandas, and sorting the values. However, I want to exclude some columns.
Input:
df = pd.DataFrame({'Number': [1, 2, 3, 4, 5], 'X' : ['X1', 'X2', 'X3', 'X3', 'X3'], 'Y' : ['Y2','Y1','Y1','Y1', 'Y2'], 'Z' : ['Z3','Z1','Z1','Z2','Z1']})
Number X Y Z
0 1 X1 Y2 Z3
1 2 X2 Y1 Z1
2 3 X3 Y1 Z1
3 4 X3 Y1 Z2
4 5 X3 Y2 Z1
Output
mode = lambda ts: ts.value_counts(sort=True).index[0]
cols = df['X'].value_counts().index
df.groupby('X')[['Y', 'Z']].agg(mode).T.reindex(columns=cols)
X3 X1 X2
Y Y1 Y2 Y1
Z Z1 Z3 Z1
But I want to exclude the X3 and X1 column. I've tried
if "X3" and "X2" in df.columns:
pass
But this doesn't do anything. Thanks for the help!