回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我正在尝试解析csv文件并打印某些时间序列图</p>
<p><em><strong>关于csv文件:</strong></em>csv文件包含大量数据,我需要根据for循环中的id处理对其的确认。csv文件如下所示:</p>
<pre><code>ID,name,date,confirmedInfections
DE2,BAYERN,2020-02-24,19
DE2,BAYERN,2020-02-25,19
DE2,BAYERN,2020-02-26,21
DE1,BADEN-WÃœRTTEMBERG,2020-02-24,1
DE1,BADEN-WÃœRTTEMBERG,2020-02-25,3
DE1,BADEN-WÃœRTTEMBERG,2020-02-26,7
</code></pre>
<p>从该文件中,我需要解析每一行并减去<code>confirmedInfections</code>,以便找到每日感染。在这个时刻,如果我选择一个有<code>ID DE2 and date 2020-03-01</code>的城市,感染是从乞讨到我选择的那一天的总和。所以,我需要编辑这个文件,以便减去并找到日常案例,以打印适当的时间序列。为了达到减法的目的,我找到了<code>pandas.shift(1)</code>方法,但它似乎不起作用</p>
<p><strong>我的代码:</strong></p>
<pre><code>def main(file):
id_array = ['DE2', 'DE1']
df = pd.read_csv(file, header='infer', parse_dates=['date'])
for key in id_array:
if (df.loc[df['ID'] == key]):
df['confirmedInfections'] = df['confirmedInfections']-df['confirmedInfections'].shift(1)
print(df)
main('data.txt')
</code></pre>
<p><strong>所需输出:</strong></p>
<pre><code>#For DE2
ID,name,date,confirmedInfections
DE2,BAYERN,2020-02-24,19
DE2,BAYERN,2020-02-25,0
DE2,BAYERN,2020-02-26,2
#For DE1
ID,name,date,confirmedInfections
DE1,BADEN-WÃœRTTEMBERG,2020-02-24,1
DE1,BADEN-WÃœRTTEMBERG,2020-02-25,2
DE1,BADEN-WÃœRTTEMBERG,2020-02-26,4
</code></pre>
<p><strong>我得到的错误:</strong></p>
<pre><code>ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
</code></pre>
<p>有什么想法吗</p>