【发布时间】:2021-05-16 13:53:53
【问题描述】:
我有一份 PDF 文档,在文档的表格中报告了某个州的县的 COVID-19 编号。我正在使用 camelot 将表格读入 pandas 数据框中,并根据第一列中的值(县名)提取各行中的值。为此,我使用布尔索引,如下所述:How do I sum values in a column that match a given condition using pandas?
我正在使用提取的数据报告我的组织感兴趣的报告中列出的部分县的 COVID-19 统计数据。我也在提取该州的总数,但 PDF 的制作者无法决定是否要将该行数据称为“Gesamt”(“Total”)或“Gesamtergebnis”(“Total result”)。在 camelot 从 PDF 中提取表格后,我正在使用的数据框如下所示:
0 1 2 3
...
9 A County 13.789 (+22) 1.566,0
10 My County 16.581 (+45) 3.040,0
11 Their County 7.445 (+15) 2.821,6
...
55 Gesamt 304.950 (+820) 2.747,2
如果他们使用“Gesamt”,则下面的代码有效。我想写它,这样如果他们使用“Gesamtergebnis”它也能工作。我不能依赖总数(“Gesamt”或“Gesamtergebnis”)总是在同一行。
# Open LGA reports for yesterday and the day before
# TO DO: Sometimes the LGA report is named COVID_Lagebericht_LGA_yymmdd.pdf or it ends in _01
# Add in a try/else statement to compensate for this
rptyes = f'Reports_LGA/{yday_yymmdd}_COVID_Tagesbericht_LGA.pdf'
rptdbf = f'Reports_LGA/{daybef_yymmdd}_COVID_Tagesbericht_LGA.pdf'
# Read the LGA reports into dataframes.
dfyes = camelot.read_pdf(rptyes, pages='2', flavor='stream')
dfdbf = camelot.read_pdf(rptdbf, pages='2', flavor='stream')
# Extract the statewide 7-D-I
# TO DO: Sometimes the last line says "Gesamt", sometimes "Gesamtergebnis" or something else.
# Add in some sort of error checking or try/else statement or regular expression to compensate
landindexyes = lambda land: dfyes[0].df.loc[dfyes[0].df[0] == land].index[0]
landindexdbf = lambda land: dfdbf[0].df.loc[dfdbf[0].df[0] == land].index[0]
land = 'Gesamt'
bwname = 'Baden-Württemberg'
bwcases = int(dfyes[0].df.loc[landindexyes(land), 1].replace('.',''))
bwcasesdiff = dfyes[0].df.loc[landindexyes(land), 2]
bwdeaths = int(dfyes[0].df.loc[landindexyes(land), 4].replace('.',''))
bwdeathsdiff = dfyes[0].df.loc[landindexyes(land), 5]
bw7diyes = float(dfyes[0].df.loc[landindexyes(land), 7].replace(',','.'))
bw7didbf = float(dfdbf[0].df.loc[landindexdbf(land), 7].replace(',','.'))
bw7didiff = bw7diyes - bw7didbf
rptrowsbw = [bwname, bwcases, bwcasesdiff, bwdeaths, bwdeathsdiff, bw7diyes, bw7didbf]
如何使用正则表达式匹配传递给 lambda 表达式“landindexyes”和“landindexdbf”的变量中的“Gesamt”或“Gesamtergebnis”?
如果正则表达式不是要走的路,我愿意接受其他建议。我认为 if/else 可能会起作用,但我认为这不会那么优雅。
【问题讨论】: