Search code examples
pythonpandasdataframetweepytwitterapi-python

How to append extra column with default value in pandas dataframe?


How do i append extra column with default value in pandas dataframe ?

please refer the code below :

userID = "narendramodi"
tweets = api.user_timeline(screen_name=userID, 
                           # 200 is the maximum allowed count
                           count=500,
                           include_rts = True,
                           # Necessary to keep full_text 
                           # otherwise only the first 140 words are extracted
                           tweet_mode = 'extended'
                           )

all_tweets = []
all_tweets.extend(tweets)
oldest_id = tweets[-1].id
while True:
    tweets = api.user_timeline(screen_name=userID, 
                           # 200 is the maximum allowed count
                           count=200,
                           include_rts = True,
                           max_id = oldest_id - 1,
                           # Necessary to keep full_text 
                           # otherwise only the first 140 words are extracted
                           tweet_mode = 'extended'
                           )
    if len(tweets) == 0:
        break
    oldest_id = tweets[-1].id
    all_tweets.extend(tweets)
    print('N of tweets downloaded till now {}'.format(len(all_tweets)))


from pandas import DataFrame
outtweets = [[
              
              tweet.id_str, 
              tweet.created_at, 
              tweet.favorite_count, 
              tweet.retweet_count,]  for idx,tweet in enumerate(all_tweets)]

df = DataFrame(outtweets,columns=["id",
                                  "created_at",
                                  "favorite_count",
                                  "retweet_count",)]
df.head(10)

Please refer the below code, it runs okay but i want to add the extra column in dataframe. suppose default value as domain = "NA" for all tweets reflecting in dataframe.


Solution

  • It is as simple as:

    df['domain'] = "NA"