不支持使用DataFrame为系列建立索引,请使用适当的DataFrame列

问题描述 投票:1回答:1

我有一个包含twitter用户名的数据框,如下所示:enter image description here

并且我想抓取他们的下列清单。因此,我编写此代码:

def searchFollowing():
    username = tmp_df[["username"]].drop_duplicates().reset_index(drop=True)
    #df_following = pd.DataFrame()
    for user in username["username"].head(5):
        twint.storage.panda.clean()
        c.Username = user
        c.Pandas = True
        c.Hide_output = True
        #c.Output = "list_following.csv"
        #c.Store_csv = True
        twint.run.Following(c)     
        Following_df = twint.storage.panda.Follow_df
list_of_following = Following_df['following'][username]
list_of_following

并且我在运行代码时收到此错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-76-a4bb2a41fa85> in <module>
----> 1 list_of_following = Following_df['following'][username]
      2 list_of_following

~\Anaconda3\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
    909             key = check_bool_indexer(self.index, key)
    910 
--> 911         return self._get_with(key)
    912 
    913     def _get_with(self, key):

~\Anaconda3\lib\site-packages\pandas\core\series.py in _get_with(self, key)
    917             return self._get_values(indexer)
    918         elif isinstance(key, ABCDataFrame):
--> 919             raise TypeError('Indexing a Series with DataFrame is not '
    920                             'supported, use the appropriate DataFrame column')
    921         elif isinstance(key, tuple):

TypeError: Indexing a Series with DataFrame is not supported, use the appropriate DataFrame column

您知道我的代码有什么问题吗?谢谢

python pandas dataframe web-scraping twitter
1个回答
0
投票

您无法用另一个数据框username编制索引,因此请尝试使用:

list_of_following = Following_df.loc[Following_df['following'].isin(username), 'following']

而不是:

list_of_following = Following_df['following'][username]
© www.soinside.com 2019 - 2024. All rights reserved.