简体   繁体   English

使用.str.contains过滤一个FRED系列的df

[英]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怎么运行的

  1. The CSV file is imported as a Pandas dataframe CSV 文件导入为 Pandas dataframe
  2. a list comprehension is used to build an array of involved countries by matching a list of US countries with the second column of the first row of the dataframe with both columns.通过将美国国家/地区列表与 dataframe 的第一行的第二列与两列进行匹配,列表理解用于构建涉及国家/地区的数组。 This array should contain only one element if only one country is mentioned.如果只提到一个国家,这个数组应该只包含一个元素。 Only the first element of the array is saved in the country variable.只有数组的第一个元素保存在country变量中。

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

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