0

I am reading in data from an excel file. And currently I am breaking down it several different DFs based on the row numbers. What I want to do is create a loop which will iterate over the imputed row numbers and create different Dfs with the appropriate suffixes. Currently I am creating separate Dfs by passing in row numbers in each line.

NHE_17= NHE_data.parse('NHE17') 
#Slice DataFrame for only Total National Health Expenditure data, from 
row 0 to 37(Population): total_nhe
total_nhe = NHE_17.iloc[0:37]
print(total_nhe.iloc[0,-1])
#Slice DataFrame for only Health Consumption Expenditures, from row 38 to 
70(Total CMS Programs (Medicaid, CHIP and Medicare): total_hce
total_hce = NHE_17.iloc[38:70]

I want to be able call the function with the row numbers and suffix to create the specific DF.

1 Answer 1

1

that function would look like:

   def row_slicer(slice_tuple):

      #This will slice the NHE_17 according to slice_parameters parameters
      # Input slice_tuple = [x1,x1 

      df_temp = NHE_17.iloc[slice_tuple[0]:slice_tuple[1]]
      return df_temp

   dict_dataframes = {}

   #assuming this is a dictionary, else you can  zip lists with pandas columns
   name_list_row = [['total_nhe',[0,37]],['total_hce',[38,70]]...]

   for name,slice_tuple in name_list_row:
      df = row_slicer(slice_tuple)
      dict_dataframes[name] = df

Hope this helps!

Sign up to request clarification or add additional context in comments.

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.