我正在从Excel文件中读取数据。目前,我根据行号将其分解为几个不同的DF。 我想做的是创建一个循环,该循环将遍历插补的行号并创建带有适当后缀的不同Df。 目前,我正在通过在每行中传递行号来创建单独的Df。
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]
我希望能够使用行号和后缀来调用该函数以创建特定的DF。
答案 0 :(得分:1)
该函数如下所示:
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
希望这会有所帮助!