0

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!

jenryb
  • 2,017
  • 12
  • 35
  • 72
  • `if "X3" and "X2" in df.columns:` is not doing what you think it's doing. – kylieCatt Jun 16 '15 at 15:30
  • possible duplicate of [How do I test one variable against multiple values?](http://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values) – kylieCatt Jun 16 '15 at 15:31

1 Answers1

0

I'm not sure if it is what you want but try

df[[c for c in df.columns if c not in ['X2','X3']]]
hvedrung
  • 467
  • 4
  • 13