Home > Back-end >  Retrieving values from a DataFrame
Retrieving values from a DataFrame

Time:05-20

My 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]:

df['market_id'].values[0]

To collect the value writing only ['market_id'] I'm using .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?

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.

CodePudding user response:

How about just converting the single-row dataframe to a python dictionary? 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:

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

Alternatively, since your data is 1D, you can use a pandas Series instead of a Dataframe:

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

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

CodePudding user response:

If you use .iloc[0] instead of .iloc[:1] then you get single row as pandas.Series and you can get value fromr Series using only header. And this doesn't need .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
  • Related