简体   繁体   English

从 DataFrame 中检索值

[英]Retrieving values from a DataFrame

My DataFrame ( df = df.sort_values('market_name').iloc[:1] ):我的 DataFrame ( df = df.sort_values('market_name').iloc[:1] ):

                  competition                         event_name  event_id country_code           market_name    market_id  total_matched             Home  Home_id            Away  Away_id Draw  Draw_id
7  CONMEBOL Copa Libertadores  Atletico MG v Independiente (Ecu)  31459931         None  First Half Goals 1.5  1.199224510  115362.090985  Under 1.5 Goals  1221385  Over 1.5 Goals  1221386             0

For get market_id i need use index [0] :对于获取market_id我需要使用索引[0]

df['market_id'].values[0]

To collect the value writing only ['market_id'] I'm using .reset_index() + .iterrows() :要收集仅写入['market_id']的值,我正在使用.reset_index() + .iterrows()

df = df.sort_values('market_name').iloc[:1]
df = df.reset_index()
for index, row in df.iterrows():
    row['market_id']

As this dataframe will always exist only one line, there is a more professional way to get this same result without this mess using many lines and looping?由于这个dataframe将始终只存在一行,有没有更专业的方法来获得相同的结果,而无需使用多行和循环来实现这一混乱?

The idea would be to format this dataframe beforehand so I don't need to put this .value[0] in each value I want to fetch and call only by the column name.我的想法是预先格式化这个dataframe所以我不需要把这个.value[0]放在我想要获取的每个值中并且只通过列名调用。

How about just converting the single-row dataframe to a python dictionary?将单行 dataframe 转换为字典 python 怎么样? You can do it like this:你可以这样做:

dct = df.to_dict(orient='records')[0];
marketId = dct['market_id']

If you want to change the dictionary back to a dataframe after modifying it, you can do this:如果你想在修改后把字典改回一个dataframe,你可以这样做:

df2 = pd.DataFrame([dct], columns=dct.keys())

Alternatively, since your data is 1D, you can use a pandas Series instead of a Dataframe:或者,由于您的数据是一维的,您可以使用 pandas 系列而不是 Dataframe:

ser = df.reset_index(drop=True).T[0]
print(ser)
print('\nmarket_id is:', ser['market_id'])

Output: Output:

competition             CONMEBOL Copa Libertadores
event_name       Atletico MG v Independiente (Ecu)
event_id                                  31459931
country_code                                  None
market_name                   First Half Goals 1.5
market_id                                 1.199225
total_matched                        115362.090985
Home                               Under 1.5 Goals
Home_id                                    1221385
Away                                Over 1.5 Goals
Away_id                                    1221386
Draw
Draw_id                                          0
Name: 0, dtype: object

market_id is: 1.19922451

If you use .iloc[0] instead of .iloc[:1] then you get single row as pandas.Series and you can get value from Series using only header. And this doesn't need .reset_index()如果您使用.iloc[0]而不是.iloc[:1]那么您将获得单行pandas.Series并且您可以仅使用 header 从Series中获取价值。这不需要.reset_index()

import pandas as pd

data = {
    'A': [1,2,3], 
    'B': [4,5,6], 
    'C': [7,8,9]
}

df = pd.DataFrame(data)

row = df.sort_values('A').iloc[0]

print('row:')
print(row)
print('---')
print('value A:', row['A'])
print('value B:', row['B'])

Result:结果:

row:
A    1
B    4
C    7
Name: 0, dtype: int64
---
value A: 1
value B: 4

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

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