Home > other >  Select numerical column header names
Select numerical column header names

Time:08-25

I have a data frame with column names in numbers 1 to 99, i use integer names to loop for different activities. Datetime in index

| datetime                 | 1     | 2    | 3    | 4    | 8 | 9 |
|--------------------------|-------|------|------|------|---|---|
| 22-07-2022 17:38:21.3821 | 27.79 | 1    | 36   | 77.9 | 0 | 1 |
| 23-07-2022 17:38:21.3821 | 21.62 | 0    | 47.6 | 24.6 | 1 | 1 |
| 24-07-2022 17:38:21.3821 | 19.56 | 1    | 53.2 | 20.2 | 1 | 1 |
| 25-07-2022 17:38:21.3821 | 19.41 | 0    | 54   | 19.7 | 1 | 1 |
| 26-07-2022 17:38:21.3821 | 19.49 | 1    | 54.2 | 19.5 | 1 | 1 |
| 27-07-2022 17:38:21.3821 | 19.48 | 1    | 54.2 | 19.8 | 1 | 1 |

Is there anyway to call the column header 2 & 8 without converting to string header.

Convert integer column name to string name

# Convert integer to string column name
df.rename(columns={2: 'set_speed', 8: 'main_spare'}, inplace=True)
df = df[(df.set_speed.isin(0)) & (df_t.main_spare.isin(1))]
# Create a dictionary
dfd = {'{0[0]}{0[1]}'.format(i): x for i, x in df.groupby(['set_speed', 'main_spare'])}

Trial 1

df = df[(df.loc[:, [2]].isin(0)) & (df.loc[:, [8]].isin(1))]
    raise TypeError(
TypeError: only list-like or dict-like objects are allowed to be passed to DataFrame.isin(), you passed a 'int'

Trial 2


df = df[(df.loc[:, [2]].isin([0])) & (df.loc[:, [8]].isin([1]))
TypeError: '<' not supported between instances of 'Timestamp' and 'int'

CodePudding user response:

Is there anyway to call the column header 2 & 8 without converting to string header.

This should work:

df.loc[:, [2, 8]]

Here, you probably want

df[(df.loc[:, 2].isin([0])) & (df.loc[:, 8].isin([1]))]
  • Related