1

I have this type of dataframe;

A = ["axa","axb","axc","axd","bxa","bxb","bxc","bxd","cxa".......]

My question is I have this type of data but there are more than 350 columns and for example i need only 'c' including column names in new dataframe. How can i do that?

new dataframe columns should look like this;

B = A[["axc","bxc","cxa","cxb","cxc","cxd","dxc","exc","fxc".......]]

2 Answers2

0

Use for filter columns names with c by DataFrame.filter:

df2 = df.filter(like='c')

Or use list comprehension for filter columns names:

df2 = df[[x for x in df.columns if 'c' in x]]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

You can do it easily using list comprehension:

new_df = df[[col for col in df.columns if 'c' in col]]
Romain Capron
  • 1,565
  • 1
  • 18
  • 23