在while循环中添加到pandas df

我有一个看起来像这样的 df,叫做 full_senator_df:


    Official Twitter    Senator         party

0   SenShelby           Richard Shelby  Republican

1   lisamurkowski       Lisa Murkowski  Republican

2   SenDanSullivan      Dan Sullivan    Republican

我已经编写了一些代码来使用这些数据来检索这些参议员的推文。是否可以将结果附加到表中或将结果作为 json 而不是它当前正在执行的打印?


senator_count = 0

num_senators = len(full_senator_df.index)


while senator_count <= num_senators:

    senator_official_twitter = full_senator_df['Official Twitter'][senator_count]

    tweets = api.user_timeline(screen_name = senator_official_twitter, count = tweet_num, include_rts = True)


    for status in tweets:

        print(full_senator_df['Senator'][senator_count], status.text, full_senator_df['party'][senator_count])


    senator_count += 1

http://img1.mukewang.com/613c63d20001ec8f22010394.jpg

慕容3067478
浏览 139回答 2
2回答

喵喔喔

以下代码创建了一个新的数据框(表),其中包含每方参议员的推文# Create an empty dataframe stub to append to laterall_tweets_df = pd.DataFrame(columns=['Senator', 'Party', 'Tweet'])# Iterate over the initial dataframefor _, row in full_senator_df.iterrows():&nbsp; &nbsp; tweets = api.user_timeline(screen_name = row['Official Twitter'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;count = tweet_num,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;include_rts = True)&nbsp; &nbsp; senator_tweets_df = pd.DataFrame({'Senator': row['Senator'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Party': row['party'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Tweet': tweets})&nbsp; &nbsp; # Append to the output&nbsp; &nbsp; all_tweets_df = pd.concat([all_tweets_df, senator_tweets_df], sort=True)输出应该是这样的&nbsp; &nbsp; &nbsp; &nbsp; Party&nbsp; &nbsp; Senator&nbsp; &nbsp;Tweet0&nbsp; Republican&nbsp; &nbsp; &nbsp;Shelby&nbsp; tweet11&nbsp; Republican&nbsp; &nbsp; &nbsp;Shelby&nbsp; tweet22&nbsp; Republican&nbsp; &nbsp; &nbsp;Shelby&nbsp; tweet30&nbsp; Republican&nbsp; Murkowski&nbsp; tweet11&nbsp; Republican&nbsp; Murkowski&nbsp; tweet22&nbsp; Republican&nbsp; Murkowski&nbsp; tweet30&nbsp; Republican&nbsp; &nbsp;Sullivan&nbsp; tweet11&nbsp; Republican&nbsp; &nbsp;Sullivan&nbsp; tweet22&nbsp; Republican&nbsp; &nbsp;Sullivan&nbsp; tweet3

心有法竹

我想你快到了。如果你想保持循环,而不是打印,你可以将该数据加载到数据帧中。首先定义一个新的数据框dfTweets = pd.DataFrame() # place this before your while looprow_num = 0while ......&nbsp; &nbsp; for status in tweets:&nbsp; &nbsp; &nbsp; &nbsp; dfTweets.loc[0, row_num] = full_senator_df['Senator'][senator_count]&nbsp; &nbsp; &nbsp; &nbsp; dfTweets.loc[1, row_num] = status.text,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; dfTweets.loc[2, row_num] = full_senator_df['party'][senator_count]&nbsp; &nbsp; &nbsp; &nbsp; row_num += 1dfTweets.columns = ["Senator", "tweet_text"]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python