Isin 函数跳过正确的值 python

Isin function skip correct value python

我正在处理来自 get API 请求的响应 json 文件。我已经能够弄清楚如何展平响应,并且我想通过包含 pdf 文件扩展名的记录来过滤相关的数据框,我将使用它来检索感兴趣的文件。 这是代码:

from flatten_json import flatten
import requests
import pandas as pd
import re
payload= {"chamber_type":"committee","chamber":"dail","date_start":"2018-01-01", "date_end":"2018-12-31", "limit":"1000"}
test = requests.get("https://api.oireachtas.ie/v1/debates", params=payload)
text = test.content.decode("utf-8")
print(text)
test.json()
test1=flatten(test.json())
df = pd.Series(test1).to_frame()
df[["pdf"]] = df[df.index.isin(["uri_pdf"])]

整个 df returns nan 即使它应该给出肯定的结果。

我试图通过相同的表达式过滤索引,但结果是一个空的 df。

isin 哪里不起作用了?

.isin() 不能像您预期的那样工作(例如包含)。 IIUC,你需要 str.contains():

df[df.index.str.contains('pdf_uri')]

或者您可以使用 str.endswith()

df[df.index.str.endswith('pdf_uri')]