繁体   English   中英

使用 WHILE 循环组合多个数据框

[英]Combine multiple Data Frames with WHILE loop

来自建议的更新代码结果的屏幕截图

“dlist”是 dataframe 中的提供程序 ID 列表。 我尝试对'dlist'使用while循环,但它只返回数组中最后一个提供者ID的值。 在这种情况下,它是 1005。我使用了 append function 但它什么也没做。 未显示提供者 ID 为 1000 的额外 74 行。 如何组合所有内容,以便显示 dlist 中的两个数字的值等于 684 行?

dlist = ["1000", "1005"]

final_list = pd.DataFrame()

index = 0

while index < len(dlist):
    provider = dlist[index]
    
    # Filter dentist (CHANGEABLE)
    final_list = report_df[(report_df["provider_id"] == provider)]

    # Sort values of the codes
    final_list = final_list.sort_values(['codes','report_month'], ascending=True)

    # Drop 'report_year' column
    final_list = final_list.drop(['report_year'], axis = 1)

    # Change 'report_month' numbers into month name
    final_list = final_list.replace({'report_month': {1: "January",
                                                      2: "February",
                                                      3: "March",
                                                      4: "April",
                                                      5: "May",
                                                      6: "June",
                                                      7: "July",
                                                      8: "August",
                                                      9: "September",
                                                      10: "October",
                                                      11: "November"}})
    final_list.append(final_list)
    index +=1

缺失值

当前代码的结果

可以创建一个包含所有数据框的列表,然后将它们连接起来。 就像在 while 循环之前有一个list_of_dfs = [] ,并且在index+=1之前添加list_of_dfs.append(final_list) 你可能不想要final_list.append(final_list) 最终可以做my_df_of_concern = pd.concat(list_of_dfs, index=0) https://pandas.pydata.org/docs/reference/api/pandas.concat.html

你的问题是你一次又一次地修改同一个变量。 在您的代码中:

Line 1: while index < len(dlist):
Line 2:    provider = dlist[index]
    
Line 3:    # Filter dentist (CHANGEABLE)
Line 4:    final_list = report_df[(report_df["provider_id"] == provider)] # PROBLEM LINE
Line 5:    # MORE CODE
Line 6:    # MORE CODE
Line 7:    final_list.append(final_list)
Line 8:    index +=1

由于您的dlist具有["1000", "1005"] ,因此在第一次运行期间,在第 4 行中, final_list具有provider_id == 1000的所有行。 然后对它进行一些修改,然后在第 7 行中,将 append 更改为相同的 object。 所以现在, final_list 将拥有所有内容的 2 个副本,因为您正在执行final_list.append(final_list)

然后你增加 index 并且对于 provider 现在是1005的下一次迭代,你再次执行第 4 行,你的 final_list 将被覆盖。 这意味着存储在该变量中的所有先前值不再存在,仅存在provider_id == 1005的新值。

尝试像这样更改您的代码

while index < len(dlist):
    provider = dlist[index]
    
    # Filter dentist (CHANGEABLE)
    report_list = report_df[(report_df["provider_id"] == provider)]

    # Sort values of the codes
    report_list = report_list.sort_values(['codes','report_month'], ascending=True)

    # Drop 'report_year' column
    report_list = report_list.drop(['report_year'], axis = 1)

    # Change 'report_month' numbers into month name
    report_list = report_list.replace({'report_month': {1: "January",
                                                      2: "February",
                                                      3: "March",
                                                      4: "April",
                                                      5: "May",
                                                      6: "June",
                                                      7: "July",
                                                      8: "August",
                                                      9: "September",
                                                      10: "October",
                                                      11: "November"}})
    final_list.append(report_list)
    index +=1

report_list充当一个临时变量,它保存特定提供者的所有数据,然后在您进行所有修改(如删除 report_year 列、排序等)之后,您将 append 值添加到 final_list。 现在,您将拥有跨多次迭代的数据。

此外,而不是做

while index < len(dlist):
    provider = dlist[index]
    index +=1

你可以这样做:

for provider in dlist:
    # YOUR CODE where provider will be "1000" for 1st run and "1005" in second run

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM