Skip to content
Advertisement

Pandas dataframe (getting one value without the index)

From a pandas dataframe, I want just the value and not the index.

state_dist = df[df['candidate'] == 'Don Young']['state_dist']

Returns:

    9    AK-00
Name: state_dist, dtype: object

Adding .to_string()

state_dist = df[df['candidate'] == 'Don Young']['state_dist'].to_string()

Returns:

'9    AK-00'

I do not want to loop, how can I just get:

'AK-00'

Advertisement

Answer

Always check the docs for the method you are using:

state_dist = df[df['candidate'] == 'Don Young']['state_dist'].to_string(index=False)
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement