[英]Pandas: iterate through a list to match values in a dataframe
I'm working with a dataframe of covid counts for all counties in the US.我正在使用 dataframe 的美国所有县的 covid 计数。 I've figured out how to isolate one county and export the result to a csv like this:
我已经想出了如何隔离一个县并将结果导出到 csv ,如下所示:
import pandas as pd
covid = pd.read_csv('https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv')
agh = covid[covid['county'] == 'Allegheny']
agh.to_csv('AlleghenyCovid.csv')
Now I want to create a list of counties like this:现在我想创建一个这样的县列表:
countyList = covid.county.unique()
and loop through them to create a csv for each.并循环遍历它们,为每个创建一个 csv。 That's where I'm stuck.
这就是我卡住的地方。 How can I use a list of known values to iterate through the dataframe and create new dataframes from each iteration?
如何使用已知值列表遍历 dataframe 并从每次迭代中创建新的数据帧? I've been thinking something like:
我一直在想这样的事情:
for i in countyList:
if covid['county'] == i:
...
but that gives an ambiguous value error.但这会产生模棱两可的值错误。 I'm not sure exactly what needs to be defined.
我不确定到底需要定义什么。
Solution iterate unique list of county
column:解决方案迭代
county
列的唯一列表:
for name in covid.county.unique()
covid.loc[covid.county == name,:].to_csv(name+'.csv')
For each county named by name
:对于以
name
命名的每个县:
covid
where county
is equal to name
covid
中选择行,其中county
等于name
name
+ .csv
.name
+ .csv
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.