Bill Roberts
Bill Roberts

Reputation: 81

Extract single value by indexing a pandas DataFrame

I am calling a row from a pandas dataframe as

row = df.iloc[[i]]
customer.customer_id = row['billing_city']

When I then call `customer.billing_city I get an output like

4    Brooklyn
Name: billing_address_city, dtype: object

I would like to just have

Booklyn

for pursposes of creating objects and such

Upvotes: 3

Views: 4060

Answers (2)

cs95
cs95

Reputation: 403218

Use df.iat to access a single item.

df.iat[i, df.columns.get_loc('billing_city')]

Uses iat for the same reason you've used iloc. If your indexes are integeral, it won't matter, and you could use df.at instead.


There's also loc + item.

df.iloc[i, df.columns.get_loc('billing_city')].item()

Upvotes: 3

jpp
jpp

Reputation: 164843

Using pd.DataFrame.at:

res = df.at[i, 'billing_city']

Assumes your dataframe is indexed by an integer range; if this isn't the case, you can use df.reset_index() prior to using this method.

Upvotes: 1

Related Questions