-3

Hi I have a data frame and I would like to access to one of my columns but only the cell of my data frame by position.

I mean, this is my df:

| A   |  B  |  C
------|-----|--
|  1  |  2  | 3
|  4  |  5  | 5
|

I want to access to the cell [0, 1] I mean "B" these are the line codes that I used

df.iloc[0,1]

But my result was 2 and I want the letter B in my result.

Thanks for your help

coding
  • 917
  • 2
  • 12
  • 25
  • I'm confused, do you want just the column name "B"? Or B and 2. – NYC Coder Aug 22 '20 at 16:54
  • Just the column name "B", cell [0,1]. But iloc only works to access to the position of the starting of the data frame, exlcudes the column position so If I said df.iloc[0,1] my result is 2 not B – coding Aug 22 '20 at 16:59
  • [Return the column name(s) for a specific value in a pandas dataframe](https://stackoverflow.com/questions/38331568) – Trenton McKinney Aug 22 '20 at 17:01

3 Answers3

0

try this

df = pd.concat([pd.DataFrame([df.columns.to_list()], columns=df.columns.to_list()), df])
df.iloc[0, 1]
Kuldip Chaudhari
  • 1,112
  • 4
  • 8
0

Column names are not part of the DataFrame that can be accessed by iloc. To access the column labels use df.columns. So a solution might be:

df.columns[1] #gives you B
Dames
  • 776
  • 3
  • 11
0

you can access the names of the columns as

df.columns

now it returns the name of columns with dtype object this can get you the value b df.columns[1]

Nav
  • 93
  • 9