-2

I have the following code:

values = ta.service(data["T"], data["H"])

print(values)

The variable values is a DataFrame and is printed as:

|            |     OT     |     OH     |
|------------|------------|------------|
| Timestamp  |            |            |
| 26-06-2020 | 19.54      |  NaN       |
| 03-07-2020 | 16.024     |  17.20     |
| 10-07-2020 | 19.26      |  25.89     |
----------------------------------------

I tried to get only the OT and OH columns in different ways, for example:

print(values.iloc[:, 1:2])

But, for this, I got:

|            |     OH     |
|------------|------------|
| Timestamp  |            |
| 26-06-2020 |  NaN       |
| 03-07-2020 |  17.20     |
| 10-07-2020 |  25.89     |
---------------------------

Shouldn't I get the columns 1 and 2, e.g., OT and OH? I also tried:

print(values.columns[1:2]) 

But I got:

Index(['OH'], dtype='object')

What am I missing?

Miguel Moura
  • 36,732
  • 85
  • 259
  • 481
  • 4
    `.iloc` works like slicing, so last index is not included. you want `df.iloc[:, :2]` for the two columns. oh, and the `Timestamp` thingie is your index, not a usual column. – Quang Hoang Aug 26 '22 at 15:15
  • `values.columns` gives you a list of your column names. If you want to obtain the specific column with values, you can do `values[values.columns[1:3]]`. –  Aug 26 '22 at 15:17
  • Refer https://stackoverflow.com/questions/11285613/selecting-multiple-columns-in-a-pandas-dataframe, for detailed explanation. – Kaushal Pahwani Aug 26 '22 at 15:17

2 Answers2

1

As mentioned by Quang Hoang, iloc works like slicing. So you could do:

values.iloc[:,0:2]

or with labels

values.loc[:,["OH", "OT"]]
Heivers
  • 36
  • 4
0

try this:

values[["OT","OH"]]
Mouad Slimane
  • 913
  • 3
  • 12