簡體   English   中英

在while循環中添加到熊貓df

[英]Adding to pandas df in while loop

我有一個看起來像這樣的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

此處電流輸出

以下代碼創建一個新的數據框(表),其中每個參議員每個黨派都有推文

# Create an empty dataframe stub to append to later
all_tweets_df = pd.DataFrame(columns=['Senator', 'Party', 'Tweet'])

# Iterate over the initial dataframe
for _, row in full_senator_df.iterrows():
    tweets = api.user_timeline(screen_name = row['Official Twitter'],
                               count = tweet_num,
                               include_rts = True)
    senator_tweets_df = pd.DataFrame({'Senator': row['Senator'],
                                      'Party': row['party'],
                                      'Tweet': tweets})
    # Append to the output
    all_tweets_df = pd.concat([all_tweets_df, senator_tweets_df], sort=True)

輸出應該是這樣的

        Party    Senator   Tweet
0  Republican     Shelby  tweet1
1  Republican     Shelby  tweet2
2  Republican     Shelby  tweet3
0  Republican  Murkowski  tweet1
1  Republican  Murkowski  tweet2
2  Republican  Murkowski  tweet3
0  Republican   Sullivan  tweet1
1  Republican   Sullivan  tweet2
2  Republican   Sullivan  tweet3

我想你快到了。 如果要保持循環,則可以打印該數據而不是打印該數據到數據框中。 首先定義一個新的數據框

dfTweets = pd.DataFrame() # place this before your while loop
row_num = 0
while ...
...
    for status in tweets:
        dfTweets.loc[0, row_num] = full_senator_df['Senator'][senator_count]
        dfTweets.loc[1, row_num] = status.text, 
        dfTweets.loc[2, row_num] = full_senator_df['party'][senator_count]
        row_num += 1

dfTweets.columns = ["Senator", "tweet_text"]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM