[英]How to append extra column with default value in pandas dataframe?
How do i append extra column with default value in pandas dataframe?我如何在 pandas dataframe 中使用默认值 append 额外列?
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.请参考下面的代码,它运行正常,但我想在 dataframe 中添加额外的列。 suppose default value as domain = "NA" for all tweets reflecting in dataframe.
假设 dataframe 中反映的所有推文的默认值为domain = "NA" 。
It is as simple as:它很简单:
df['domain'] = "NA"
It'll fill new_col with NaN values.它将用 NaN 值填充 new_col。
import numpy as np
df['new_col'] = np.NaN
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.