[英]Using .str.contains to filter a df of FRED series
I am trying to download a data series for each state from the FRED api. i have loaded all the data series containing 'Housing Inventory: Active Listing Count state' into a df however there are still over 1000+ rows.我正在尝试从 FRED api 下载每个 state 的数据系列。我已将包含“房屋库存:活动列表状态”的所有数据系列加载到 df 中,但是仍然有超过 1000 行。 Is there a way i can search the title of each series to see if it contains the name of a state?
有没有办法可以搜索每个系列的标题,看看它是否包含 state 的名称?
i have tried我努力了
df=df.loc[df['title'].str.contains(["Alaska","Alabama",...,"Wyoming"])]
Series ID = ACTLISCOU系列 ID = ACTLISCOU
Assuming you have a list with all the states, you can define a custom function to filter your title
column and use it calling pd.Series.apply
:假设您有一个包含所有状态的列表,您可以定义一个自定义 function 来过滤您的
title
列并使用它调用pd.Series.apply
:
state_list = ["Alaska","Alabama",...,"Wyoming"]
def my_filter(value):
# return True if any state is in the value
return any(state in value for state in state_list)
# Call apply to filter DF based on True|False by your filter
df_filtered = df[df['title'].apply(my_filter)]
The following code returns the country contained in the ACTLISCOUXX dataset, in this case California:以下代码返回 ACTLISCOUXX 数据集中包含的国家/地区,在本例中为加利福尼亚州:
df = pd.read_csv('ACTLISCOUCA.csv',sep=';',header=None)
us_country_list=["Arizona","California","Oregon"]
country=[i for i in us_country_list if i in df.dropna().iloc[0][1]][0]
print(country)
How it works怎么运行的
country
variable.country
变量中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.