Skip to content
Advertisement

Pandas: select cell value using pd.at with condition

I’d like to select specific cell values from a Pandas Dataframe. I want to filter out rows with specific values in column A, and then get the values from column B.

From what I understand, the correct way to do this is to use df.at, so I’ve tried

df.at(df['Column A' == column_A_value]['Column B'])

but this doesn’t work. I’ve also tried, as a hack,

str(['Column A' == column_A_value]['Column B'])

doesn’t work either.

What is the right way to do this?

Edit:

What I’m trying to do is to output the values inside of a bigger loop, something like:

while condition:
    for val in some_column_A_values:
        print("{},{}".format('stuff', df.at(df['Column A' == val]['Column B'])), file=myfile)

Advertisement

Answer

As Anton vBR has pointed out, this should solve your problem

df.loc[df['Column A'] == column_A_value]['Column B']

And if you like to get just one value you can do:

list(df.loc[df['Column A'] == column_A_value]['Column B'])[0]
Advertisement