简体   繁体   中英

get a value from a specific column and row to a variable - Pandas DataFrame

I'm storing the result of a SQL Query in a DataFrame and I just don't understand how can I get a specific value. I have the following code:

    for date in years:
    partyList = pd.read_sql_query(
        "SELECT top 2 country_name,election_date,party_name_english, vote_share, left_right FROM view_election WHERE country_name_short like '" + country + "' and election_date like '" + date + "' ORDER BY vote_share DESC",
        conn)
    parties = str(partyList).replace(" ", "").split()
    parties.remove(parties[0])
    format(parties)
    # print(partyList)
    total = total.append(partyList)
print(total.iloc[0, 0:1])

The output is:

country_name    Slovakia
Name: 0, dtype: object

How can I get the String "Slovakia" to a variable? I want to understand how I do it so I can use it for other values as well

Also, How can I store an entire row into a "normal" array? I'm asking this just because I find it easier to work with regular arrays

total.iloc[0, 0:1] selects a slice of columns at row 0. If you only want the entry at [0,0] , either use

total.iloc[0, 0]

or

total.loc[0, 'country_name']

If you want to store a row into an array, you can use to_numpy method:

arr = total.iloc[0].to_numpy()

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