How to get every nth column in pandas?

I have a dataframe that looks like this:

a1 b1 c1 a2 b2 c2 a3 ... x 1.2 1.3 1.2 ... ... ... ... y 1.4 1.2 ... ... ... ... ... z ... 

I want to group every nth column. In other words, I want a data framework with all as, one with bs and one with cs

  a1 a2 a4 x 1.2 ... ... y z 

In another SO question, I saw that you can do df.iloc[::5,:] , for example, to get every 5th raw material. Of course, I could do df.iloc[:,::3] to get c cols, but it does not work to get a and b.

Any ideas?

+10
source share
3 answers

cut columns:

 df[df.columns[::2]] 

To get every nth column

Example:

 In [2]: cols = ['a1','b1','c1','a2','b2','c2','a3'] df = pd.DataFrame(columns=cols) df Out[2]: Empty DataFrame Columns: [a1, b1, c1, a2, b2, c2, a3] Index: [] In [3]: df[df.columns[::3]] Out[3]: Empty DataFrame Columns: [a1, a2, a3] Index: [] 

You can also filter using startswith :

 In [5]: a = df.columns[df.columns.str.startswith('a')] df[a] Out[5]: Empty DataFrame Columns: [a1, a2, a3] Index: [] 

and do the same for b cols and c cols etc.

You can get a set of all unique col prefixes using the following:

 In [19]: df.columns.str.extract(r'([a-zA-Z])').unique() Out[19]: array(['a', 'b', 'c'], dtype=object) 

You can then use these values ​​to filter columns using startswith

+17
source

The following should work:

 df.ix[:, ::2] - get every second column, beginning with first (here all a's) df.ix[:, 1::2] - get every second column, beginning with second (b's) .... 

I was just looking for a solution to the same problem and solved it.

+1
source

In the current version (0.24), this works:

Getting your "a" columns:

 df.iloc[:, ::3] 

get your columns 'b':

 df.iloc[:, 1::3] 

get your 'c' columns:

 df.iloc[:, 2::3] 
0
source

Source: https://habr.com/ru/post/1244855/


All Articles