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
JavaScript
x
9
1
| datetime | 1 | 2 | 3 | 4 | 8 | 9 |
2
|--------------------------|-------|------|------|------|---|---|
3
| 22-07-2022 17:38:21.3821 | 27.79 | 1 | 36 | 77.9 | 0 | 1 |
4
| 23-07-2022 17:38:21.3821 | 21.62 | 0 | 47.6 | 24.6 | 1 | 1 |
5
| 24-07-2022 17:38:21.3821 | 19.56 | 1 | 53.2 | 20.2 | 1 | 1 |
6
| 25-07-2022 17:38:21.3821 | 19.41 | 0 | 54 | 19.7 | 1 | 1 |
7
| 26-07-2022 17:38:21.3821 | 19.49 | 1 | 54.2 | 19.5 | 1 | 1 |
8
| 27-07-2022 17:38:21.3821 | 19.48 | 1 | 54.2 | 19.8 | 1 | 1 |
9
Is there anyway to call the column header 2 & 8 without converting to string header.
Convert integer column name to string name
JavaScript
1
6
1
# Convert integer to string column name
2
df.rename(columns={2: 'set_speed', 8: 'main_spare'}, inplace=True)
3
df = df[(df.set_speed.isin(0)) & (df_t.main_spare.isin(1))]
4
# Create a dictionary
5
dfd = {'{0[0]}{0[1]}'.format(i): x for i, x in df.groupby(['set_speed', 'main_spare'])}
6
Trial 1
JavaScript
1
2
1
df = df[(df.loc[:, [2]].isin(0)) & (df.loc[:, [8]].isin(1))]
2
JavaScript
1
3
1
raise TypeError(
2
TypeError: only list-like or dict-like objects are allowed to be passed to DataFrame.isin(), you passed a 'int'
3
Trial 2
JavaScript
1
2
1
df = df[(df.loc[:, [2]].isin([0])) & (df.loc[:, [8]].isin([1]))
2
JavaScript
1
2
1
TypeError: '<' not supported between instances of 'Timestamp' and 'int'
2
Advertisement
Answer
Is there anyway to call the column header 2 & 8 without converting to string header.
This should work:
JavaScript
1
2
1
df.loc[:, [2, 8]]
2
Here, you probably want
JavaScript
1
2
1
df[(df.loc[:, 2].isin([0])) & (df.loc[:, 8].isin([1]))]
2