简体   繁体   English

从生成器 object 获取列

[英]Get columns from generator object

I'm using Scrapetube to get videos from a channel, and it brings a generator object.我正在使用 Scrapetube 从频道获取视频,它带来了一个生成器 object。 From the very simple documentation, I know it includes the parameter "videoId", but how can I know what other parameters I can get from there?从非常简单的文档中,我知道它包含参数“videoId”,但我怎么知道我可以从那里得到哪些其他参数? Can I transform a generator object into, say, a dataframe?我可以将生成器 object 转换为 dataframe 吗?

Generators allow you to efficiently iterate over (potentially infinite) sequences.生成器允许您有效地迭代(可能是无限的)序列。

In your case, you probably want to first convert the generator into a list to expose all items in the sequence.在您的情况下,您可能希望首先将生成器转换为列表以公开序列中的所有项目。

Then you can inspect what the returned elements look like and extract the information you need.然后您可以检查返回的元素的外观并提取您需要的信息。

You can then create a dataframe for instance from a list of dictionaries:然后,您可以从字典列表中创建 dataframe :

result_gen = scrapetube.xxx()
result_list = list(result_gen)

# Inspect first element
print(result_list[0])
# Inspect attributes of the first element
print(dir(result_list[0]))

# Convert/extract information of interest into a dictionary
def to_dict(element):
    ...

result_df = pd.DataFrame([to_dict(element) for element in result_list])

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

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