简体   繁体   中英

Python Pandas Dataframe Pulling cell value of Column B based on Column A

struggling here. Probably missing something incredibly easy, but beating my head on my desk while trying to learn Python and realizing that's probably not going to solve this for me.

I have a dataframe df and need to pull the value of column B based on the value of column A.

Here's what I can tell you of my dataset that should make it easier. Column A is unique (FiscalYear) but despite being a year was converted to_numeric . Column B is not specifically unique (Sales) and like Column A was converted to to_numeric . This is what I have been trying as I was able to do this when finding the value of sales using idx max. However at a specific value, this is returning an error:

v = df.at[df.FiscalYear == 2007.0, 'Sales']

I am getting ValueError: At based indexing on an integer index can only have integer indexers I am certain that I am doing something wrong, but I can't quite put my finger on it.

And here's the code that is working for me.

v = df.at[df.FiscalYear.idxmax(), 'Sales']

No issues there, returning the proper value, etc.

Any help is appreciated. I saw a bunch of similar threads, but for some reason searching and blindly writing lines of code is failing me tonight.

you can use .loc method

df.Sales.loc[df.FiscalYear==2007.0]

this will be pandas series type object. if you want it in a list you can do:

df.Sales.loc[df.FiscalYear==2007.0].tolist()

你可以尝试一下:

v = df.at[df.FiscalYear.eq(2007.0).index[0], 'Sales']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM