[PYTHON] [pandas] When specifying the default Index label in the at method, "" is not required

Introduction

I'm using DataFrame and couldn't find a site that explains the title, so I'll share it. How to specify the default Index label.

How to use the basic at method

First, prepare a suitable DataFrame.

import pandas as pd
import numpy as np

df = pd.DataFrame(np.arange(12).reshape(3, 4),
                  columns=['col_0', 'col_1', 'col_2', 'col_3'],
                  index=['row_0', 'row_1', 'row_2'])

↓ Such a DataFrame is created. スクリーンショット 2020-11-16 18.16.55.png To get the value of the col_2 column of the row_1 row from this DataFrame, do as follows.

df.at["row_1", "col_2"]
# => 6

So far, the basic usage of the at method.

Main subject

But what if the Index label isn't specified?

import pandas as pd
import numpy as np

df = pd.DataFrame(np.arange(12).reshape(3, 4),
                  columns=['col_0', 'col_1', 'col_2', 'col_3'])

↓ This is the DataFrame. スクリーンショット 2020-11-16 18.23.54.png There is no Index label. (Index names are 0, 1, 2 by default)

What should I do if I want to get the values of the 1th row and col_2th column of this DataFrame? Actually, I was able to get it with the following writing style.

df.at[1, "col_2"]
# => 6

** It seems that 1 does not need""**.

df.at["1", "col_2"]
# =>error!

Please note that this will result in an error. Until now, I thought that I had to specify the index with the iat method and iloc method, but you can also specify the label with the default index!

finally

This time, I shared the method of specifying the default Index label with the at method. Thank you for reading to the end. If you have any questions, please feel free to comment.

Recommended Posts

[pandas] When specifying the default Index label in the at method, "" is not required
When the selected object in bpy.context.selected_objects is not returned
Be careful when specifying the default argument value in Python3 series
Sequential processing method when there is not enough memory in Keras
[Golang] "package exec is not in GOROOT" when executing the test
Build Python environment on Ubuntu (when pip is not the default)
When the target is Ubuntu 16.04 in Ansible
Is there NaN in the pandas DataFrame?
Differences in the behavior of each LL language when the list index is skipped
Pipfile is not created in the current directory
When searching is not working in GAE's Datastore
When the axis and label overlap in matplotlib
What to do when is not in the sudoers file.This incident will be reported.
Embedding in datetime when only the time is known
When "ERROR: HTTP is not supported." Is displayed in mpsyt
The copy method of pandas.DataFrame is deep copy by default
About the matter that nosetests does not pass when __init__.py is created in the project directory
Test.py is not reflected on the web server in Python3.
[Pandas] If the first row data is in the header in DataFrame
Get the value while specifying the default value from dict in Python
Get the class name where the method is defined in the decorator
Numerical approximation method when the calculation of the derivative is troublesome
The minimum methods to remember when aggregating data in Pandas