Jumo
Jumo

Reputation: 23

How to pick a specific columns of header of even number only?

I want to ask a question on how to call on the specific columns that only contains an even numbers.

On my previous questions :How to make all the rows data drop the similar data and multiplying float numbers.

df2 =df['hlogUs_dB'].str.split('[,:]',expand = True)
df2 = data.drop(["0"])
df2
 0   1   2   3   4   5   6   7   8   9   ... 276 277 278 279 280 281 282 283 284 285
0   109 -3.4    110 -3.4    111 -3.4    112 -3.5    113 -3.5    ... 343 -4.3    344 -4.3    345 -4.2    346 -4.2    347 -4.2
1   109 -3.5    110 -3.5    111 -3.4    112 -3.4    113 -3.4    ... 343 -4.1    344 -4.2    345 -4.4    346 -4.4    347 -4.2
2   109 -3.7    110 -3.7    111 -3.8    112 -3.8    113 -3.8    ... 343 -4.2    344 -4.3    345 -4.3    346 -4.3    347 -4.3
3   109 -3.5    110 -3.6    111 -3.6    112 -3.6    113 -3.7    ... 343 -4.1    344 -4.1    345 -4.1    346 -4.1    347 -4.1
4   109 -3.7    110 -3.8    111 -3.8    112 -3.8    113 -3.8    ... 343 -4.2    344 -4.2    345 -4.2    346 -4.2    347 -4.3
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
165 109 -5.2    110 -5.3    111 -5.5    112 -5.7    113 -5.9    ... 343 -5.4    344 -5.3    345 -5.2    346 -5.1    347 -5.1
166 109 -5.5    110 -5.6    111 -5.8    112 -6.1    113 -6.3    ... 343 -5.5    344 -5.4    345 -5.3    346 -5.2    347 -5.2
167 109 -6.0    110 -6.2    111 -6.4    112 -6.7    113 -7.1    ... 343 -4.9    344 -4.9    345 -4.9    346 -4.9    347 -4.9
168 109 -5.4    110 -5.5    111 -5.7    112 -5.9    113 -6.2    ... 343 -5.9    344 -5.7    345 -5.7    346 -5.6    347 -5.6
169 109 -5.9    110 -6.1    111 -6.4    112 -6.6    113 -7.0    ... 343 -5.7    344 -5.7    345 -5.7    346 -5.6    347 -5.6
170 rows × 286 columns

My question is how to called out on even number without using a manual way of typing all the even number of the head of columns.

such as:

df2[[0,2,4]]*= 2

I am currently stuck on the ideas on making the conditional on the header columns. I want to call on even numbers only. I hope to find a suitable solutions on the questions. Thank you in advance.

Upvotes: 0

Views: 172

Answers (1)

Henry Ecker
Henry Ecker

Reputation: 35676

We can select all columns based on whose modulus 2 is 0 (even):

even_cols = df.columns[(df.columns % 2) == 0]

even_cols:

Int64Index([  0,   2,   4,   6,   8,  10,  12,  14,  16,  18,
            ...
            266, 268, 270, 272, 274, 276, 278, 280, 282, 284],
           dtype='int64', length=143)

Then operations can use the newly created index:

df[even_cols] *= 2

df:

     0    1    2    3    4    5    6    ...  279  280  281  282  283  284  285
0    198   78  122   16  146    8  124  ...    8  102   61  168   52  148   25
1     18   31   78   59   44   80  116  ...   75  124   51    4   96   38    7
2    152   66  112    0  114   31  172  ...   18  186   19   84   29   36    0
3     80   99  152   25   34   31  106  ...   59  190   33   68   31   66   83
4    192   95   48   95  130   14    8  ...    6   74   79   40   46  198   65
..   ...  ...  ...  ...  ...  ...  ...  ...  ...  ...  ...  ...  ...  ...  ...
165  170   58  110   75  196   11   50  ...   54   46   53  146   62   30   48
166   54    8  148   25  174   40  114  ...    6  180   32   94   44  142   16
167   42   48   48   31  126   60   86  ...   11  128   10  162   67  142   13
168   54   37   70    2  128   38  134  ...   85  166   40  142   57   54   52
169  164   41  146   40   64   44   28  ...   83   90   86  188   23   38   35

If we need every other column instead of columns based on numeric value, we can set the step of slicing to create a list of columns:

every_other_column = df.columns[::2]
df[every_other_column] *= 2

Or simply modify the DataFrame without creating a list of columns:

df.loc[:, ::2] *= 2

Sample DataFrame:

import numpy as np
import pandas as pd

np.random.seed(5)
df = pd.DataFrame(np.random.randint(0, 100, (170, 286)))
print(df)

df:

     0    1    2    3    4    5    6    ...  279  280  281  282  283  284  285
0     99   78   61   16   73    8   62  ...    8   51   61   84   52   74   25
1      9   31   39   59   22   80   58  ...   75   62   51    2   96   19    7
2     76   66   56    0   57   31   86  ...   18   93   19   42   29   18    0
3     40   99   76   25   17   31   53  ...   59   95   33   34   31   33   83
4     96   95   24   95   65   14    4  ...    6   37   79   20   46   99   65
..   ...  ...  ...  ...  ...  ...  ...  ...  ...  ...  ...  ...  ...  ...  ...
165   85   58   55   75   98   11   25  ...   54   23   53   73   62   15   48
166   27    8   74   25   87   40   57  ...    6   90   32   47   44   71   16
167   21   48   24   31   63   60   43  ...   11   64   10   81   67   71   13
168   27   37   35    2   64   38   67  ...   85   83   40   71   57   27   52
169   82   41   73   40   32   44   14  ...   83   45   86   94   23   19   35

Upvotes: 1

Related Questions