Python:如何在表中添加动态列?
目前,我的代码创建了一个包含不同列(标题、日期等)的表,其中可以找到 Google 新闻关键字的新闻。例如,在我的代码中,您首先可以看到关键字“Mcdonalds”。我们假设表的前四行包含有关麦当劳的新闻。现在我希望有另一个名为“关键字”的列,在有关麦当劳的新闻旁边始终有我的代码中关于麦当劳新闻的每一行中的关键字(麦当劳)。所有关键字都应该是相同的。如果第 5-10 行有有关 Burger King 的消息,则关键字 Burger King 应位于第 5-10 行的“关键字”列中。
import ssl
import smtplib
from GoogleNews import GoogleNews
ssl._create_default_https_context = ssl._create_unverified_context
googlenews = GoogleNews()
googlenews.set_encode('utf_8')
googlenews.set_lang('en')
googlenews.set_period('7d')
googlenews.get_news("Mcdonalds")
googlenews.get_news("Burger King")
googlenews.get_news("New York")
keys = googlenews.results()[0].keys()
Table = []
for row in googlenews.results():
Table.append({'Keyword': row['??'], 'Title': row['title'], 'Date': row['date'], 'Link': row['link'], 'Source': row['site']})
import pandas as pd
from email.message import EmailMessage
df = pd.DataFrame(Table)
´´´
Currently my code creates a table with different columns (Title, Date, etc.) where the news of the Google News keywords can be found. For example, in my code you can see the keyword "Mcdonalds" in the first place. Let's assume that the first four rows of the table contain news about MCdonalds. Now I want there to be another column called Keyword, where next to the news about Mcdonalds there is always the keyword (Mcdonalds) from my code in every row about the news of Mcdonalds. The same should be possible for all keywords. If line 5-10 has messages about Burger King, the keyword Burger King should be in the column Keyword in line 5-10.
import ssl
import smtplib
from GoogleNews import GoogleNews
ssl._create_default_https_context = ssl._create_unverified_context
googlenews = GoogleNews()
googlenews.set_encode('utf_8')
googlenews.set_lang('en')
googlenews.set_period('7d')
googlenews.get_news("Mcdonalds")
googlenews.get_news("Burger King")
googlenews.get_news("New York")
keys = googlenews.results()[0].keys()
Table = []
for row in googlenews.results():
Table.append({'Keyword': row['??'], 'Title': row['title'], 'Date': row['date'], 'Link': row['link'], 'Source': row['site']})
import pandas as pd
from email.message import EmailMessage
df = pd.DataFrame(Table)
´´´
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论