0

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
5
  • So you have a df that contains all the related values (country, daycount, etc...) and another df that only has the country column while the rest of columns blank? Commented Feb 12, 2020 at 14:53
  • No. I have only one df and I want to populate these values in the aforesaid variables. Country should be the key to populate these values. May we can create dictionary and access values. something like that... Commented Feb 12, 2020 at 14:56
  • So the df consists of only one column initially (country)? And according to which data should the other columns of the df be populated? Commented Feb 12, 2020 at 14:59
  • Df has all 4 columns including country and I want to use country as key parameter to populate remaining values. For example if I want to get day_count of germany, it should populate 'ActualFixed...and so on Commented Feb 12, 2020 at 15:04
  • If an answer was correct according to your problem, you can mark it as correct by clicking on the tick button next to it (please note that this is different from the upvote button). Thanks :) Commented Feb 13, 2020 at 17:02

3 Answers 3

1

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'
Sign up to request clarification or add additional context in comments.

Comments

0

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')

Comments

0

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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.