简体   繁体   English

从特定列和行获取值到变量 - Pandas DataFrame

[英]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.我将 SQL 查询的结果存储在 DataFrame 中,我只是不明白如何获得特定值。 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: output 是:

country_name    Slovakia
Name: 0, dtype: object

How can I get the String "Slovakia" to a variable?如何将字符串“Slovakia”获取到变量? 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我问这个只是因为我发现使用常规 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:1]选择第 0 行的列切片。如果您只想要[0,0]处的条目,请使用

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:如果要将一行存储到数组中,可以使用to_numpy方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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