1

I'm having a pandas issue.

I have a dataframe that looks like the following:

     A      B       C      D
0   max   kate    marie   John
1   kate  marie   max     john
2   john  max     kate    marie
3   marie john    kate    max

And I need to access, for instance, the cell in row 0, column D.

I tried using df.iloc[0, 3] but it returns the whole D column.

Any help would be appreciated.

HRDSL
  • 711
  • 1
  • 5
  • 22

4 Answers4

1

You could use

df.iloc[0]['D']

or

df.loc[0,'D']

Documentation reference DataFrame.iloc

To get the value at a location.

Sundeep Pidugu
  • 2,377
  • 2
  • 21
  • 43
  • you are likely to get [this](https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas) warning if you do a chained assignment. It should be avoided – anky Aug 02 '19 at 10:27
  • 1
    @anky_91 what about using `df.loc[0,'D']` ? – Sundeep Pidugu Aug 02 '19 at 10:30
0
df.iloc[0]["D"]

seems to do the trick

davo777
  • 286
  • 2
  • 15
  • you are likely to get [this](https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas) if you do a chained assignment – anky Aug 02 '19 at 10:26
0

You can refer to this for your solution

# Import pandas package
import pandas as pd

# Define a dictionary containing employee data
data = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'],
        'Age': [27, 24, 22, 32],
        'Address': ['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],
        'Qualification': ['Msc', 'MA', 'MCA', 'Phd']}

# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
print(pd)

Then you got the table like this

enter image description here

if you want the name in the 0-th row and in 0-th column("Name")

synthax = dataframe.iloc[row-index]['ColumnName']

print(df.iloc[0]['Name'])
0

It works fine if your Dataframe name is df.

df.iloc[0,3]
Out[15]: 'John'
Sundeep Pidugu
  • 2,377
  • 2
  • 21
  • 43