Pandas Series property: at
Access a single value for a row/column label pair in Pandas
The at property is used to access a single value for a row/column label pair.
Similar to loc, in that both provide label-based lookups. Use at if you only need to get or set a single value in a DataFrame or Series.
Syntax:
Series.at
Raises: KeyError
When label does not exist in DataFrame
Example:
Python-Pandas Code:
import numpy as np
import pandas as pd
df = pd.DataFrame([[0, 4, 5], [0, 6, 7], [20, 30, 40]],
index=[1, 2, 3], columns=['P', 'Q', 'R'])
df
Output:
P Q R 1 0 4 5 2 0 6 7 3 20 30 40
Example - Get value at specified row/column pair:
Python-Pandas Code:
import numpy as np
import pandas as pd
df = pd.DataFrame([[0, 4, 5], [0, 6, 7], [20, 30, 40]],
index=[1, 2, 3], columns=['P', 'Q', 'R'])
df.at[2, 'Q']
Output:
6
Example - Set value at specified row/column pair:
Python-Pandas Code:
import numpy as np
import pandas as pd
df = pd.DataFrame([[0, 4, 5], [0, 6, 7], [20, 30, 40]],
index=[1, 2, 3], columns=['P', 'Q', 'R'])
df.at[2, 'Q'] = 20
df.at[2, 'Q']
Output:
20
Example - Get value within a Series:
Python-Pandas Code:
import numpy as np
import pandas as pd
df = pd.DataFrame([[0, 4, 5], [0, 6, 7], [20, 30, 40]],
index=[1, 2, 3], columns=['P', 'Q', 'R'])
df.at[2, 'Q'] = 20
df.loc[3].at['Q']
Output:
30
Previous: Values in Pandas Series or Index
Next: Access a single value for a row/column pair in Pandas
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/pandas/series/series-at.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics