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
JavaScript
x
2
1
df.at(df['Column A' == column_A_value]['Column B'])
2
but this doesn’t work. I’ve also tried, as a hack,
JavaScript
1
2
1
str(['Column A' == column_A_value]['Column B'])
2
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:
JavaScript
1
4
1
while condition:
2
for val in some_column_A_values:
3
print("{},{}".format('stuff', df.at(df['Column A' == val]['Column B'])), file=myfile)
4
Advertisement
Answer
As Anton vBR has pointed out, this should solve your problem
JavaScript
1
2
1
df.loc[df['Column A'] == column_A_value]['Column B']
2
And if you like to get just one value you can do:
JavaScript
1
2
1
list(df.loc[df['Column A'] == column_A_value]['Column B'])[0]
2