[英]Can't change column name when creating dataframe with list comprehension
我目前正在使用 Tweepy,我用這個制作了 dataframe
df_user = pd.DataFrame([x.user.id, x.user.name, x.user.screen_name,
x.user.location, x.user.created_at, x.user.followers_count, x.user.friends_count,
x.user.verified] for x in result
)
我得到的 dataframe 有 0、1、2 等作為列名。 當我想改變它們時,這就是我所做的
df_user = df_user.rename(columns={0:'userid', 1:'name', 2:'screenname', 3:'location', 4:'acccreated',
5:'follower', 6:'friend', 7:'verified'})
因為當我嘗試
df_user = pd.DataFrame([x.user.id, x.user.name, x.user.screen_name,
x.user.location, x.user.created_at, x.user.followers_count, x.user.friends_count,
x.user.verified] for x in result, columns=['userid', 'name', 'screenname', 'location',
'acccreated','follower','friend','verified']
)
我在 for 循環中遇到語法錯誤。 我也嘗試將 for 語句放在列表中,也不起作用。 有沒有辦法在一行中同時創建 dataframe 和更改列名?
把它們放在一條線上
df = pd.DataFrame([x.user.id, x.user.name, x.user.screen_name, x.user.location, x.user.created_at, x.user.followers_count, x.user.friends_count, x.user.verified for x in result]).rename(columns={0:'userid', 1:'name', 2:'screenname', 3:'location', 4:'acccreated', 5:'follower', 6:'friend', 7:'verified'})
再加上你有一個語法錯誤,因為你的]
在錯誤的地方。 ]
應該在未verified
result
之后
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.