Akash
Akash

Reputation: 59

Python : How to set dataframe as parameter to a function in python?

I have 4 columns in CSV and I want to set CSV as parameter to a function in python. The 'key' should be my first column in CSV.

df = pd.DataFrame({'Country': ['US','France','Germany'],'daycount':['Actual360','Actual365','ActaulFixed'],'frequency':['Annual','Semi','Quart'], 'calendar':['United','FRA','Ger'})

From the above data frame I want to set parameter to the following variables, based on 'Country' as key in the dataframe and it should populate the corresponding values in following variables. I need some function or loop through which I can populate values. These values will further used in next program.

day_count = Actual360
comp_frequency = Annual
gl_calendar = UnitedStates

Upvotes: 0

Views: 2304

Answers (3)

Adam B.
Adam B.

Reputation: 192

If I understood correctly:

def retrieve_value(attribute, country, df): #input attribute and country as str
    return df.loc[df['Country'] == country, attribute].iloc[0]

Ex:

retrieve_value('daycount', 'Germany', df) -> 'ActualFixed'

Upvotes: 1

Grégoire Roussel
Grégoire Roussel

Reputation: 947

I'm not sure I got your question, let me try to reformulate it.

You have a pandas DataFrame with 4 columns, one of which (Country) acts as an index (=primary key in DB language). You would like to iterate on all the rows, and retrieve for each row the corresponding values in the other 3 columns.

If I didn't betray your intent, here is a code that'll do the job. Note that DataFrame.set_index(<column_name>) function, it tells pandas that this column should be used to index the rows (instead of the default numeric one).

In [1]: import pandas as pd                                

In [2]: df = pd.DataFrame({'Country': ['US','France','Germany'],'daycount':['Actual360','Actual365','ActaulFixed'],'frequency':['Annual','Semi','Quart'], 'calendar':['United','FRA','Ger']}).set_index('Country')                            

In [3]: df                                                 
Out[3]: 
            daycount frequency calendar
Country                                
US         Actual360    Annual   United
France     Actual365      Semi      FRA
Germany  ActaulFixed     Quart      Ger

In [4]: for country, attributes in df.iterrows(): 
   ...:     day_count = attributes['daycount'] 
   ...:     comp_frequency = attributes['frequency'] 
   ...:     # idem for the last value
   ...:     print(f"{country} -> {day_count}, {comp_frequency}") 
   ...:                                                    
US -> Actual360, Annual
France -> Actual365, Semi
Germany -> ActaulFixed, Quart

In [5]: df.loc['US', 'daycount'] # use df.loc[<country>, <attribute>] to retrieve specific value
Out[5]: 'Actual360'

Upvotes: 0

X Zhang
X Zhang

Reputation: 1325

This?

def populate(df, country):
    day_count=df[df['Country']==country]['daycount'][0]
    comp_frequency=df[df['Country']==country]['frequency'][0]
    gl_calendar=df[df['Country']==country]['calendar'][0]
    return (day_count, comp_frequency, gl_calendar)

populate(df,'US')

Out: ('Actual360', 'Annual', 'United')

Upvotes: 0

Related Questions