第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在while循環(huán)中添加到pandas df

在while循環(huán)中添加到pandas df

慕容3067478 2021-09-11 16:07:22
我有一個看起來像這樣的 df,叫做 full_senator_df:    Official Twitter    Senator         party0   SenShelby           Richard Shelby  Republican1   lisamurkowski       Lisa Murkowski  Republican2   SenDanSullivan      Dan Sullivan    Republican我已經(jīng)編寫了一些代碼來使用這些數(shù)據(jù)來檢索這些參議員的推文。是否可以將結(jié)果附加到表中或?qū)⒔Y(jié)果作為 json 而不是它當(dāng)前正在執(zhí)行的打?。縮enator_count = 0num_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
查看完整描述

2 回答

?
喵喔喔

TA貢獻1735條經(jīng)驗 獲得超5個贊

以下代碼創(chuàng)建了一個新的數(shù)據(jù)框(表),其中包含每方參議員的推文


# 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)

輸出應(yīng)該是這樣的


        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


查看完整回答
反對 回復(fù) 2021-09-11
?
心有法竹

TA貢獻1866條經(jīng)驗 獲得超5個贊

我想你快到了。如果你想保持循環(huán),而不是打印,你可以將該數(shù)據(jù)加載到數(shù)據(jù)幀中。首先定義一個新的數(shù)據(jù)框


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"]


查看完整回答
反對 回復(fù) 2021-09-11
  • 2 回答
  • 0 關(guān)注
  • 152 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號