【问题标题】:trying to use groupby() function but keep getting "TypeError: list indices must be integers or slices, not str"尝试使用 groupby() 函数但不断收到“TypeError:列表索引必须是整数或切片,而不是 str”
【发布时间】:2020-09-21 10:50:20
【问题描述】:

编辑:我在“event_id”前面添加了“columns”,因为我之前尝试仅使用“event_id”的代码时一直收到“keyerror:“event_id”。我现在取消了“columns”代码,但我仍然得到相同的keyerror;我检查了“event_id”,它被python识别为一列......有什么建议吗??

我正在尝试根据“event_id”将 df_userpolice 聚合到类别中,然后为每个 event_id 聚合所有其他数字(每个 event_id 的平均关注者数量等);然后我需要将它与较小的数据框 df_eventpolice 合并。我已将 event_id 中的每一行更改为 excel 中的整数,但由于某种原因它仍然无法正常工作,这是我的代码:

import pandas as pd
import dateutil
df_userpolice = pd.read_csv(filepath_or_buffer='userpolice.csv', error_bad_lines=False)
df_eventpolice = pd.read_csv(filepath_or_buffer='eventpolice.csv', index_col = 0)
columns = ['event_id', 'city_indiv', 'post_id_indiv', 'content_indiv', 'content_media', 'is_same_event', 'post_id_media', 'prov_code', 'date_indiv', 'geolocation', 'issue_type_indiv', 'followers_count', 'fan_count', 'gender', 'status_count', 'issue_type_words_indiv',  'action_form_indiv', 'action_form_words_indiv', 'username', 'city_media', 'uid', 'verified', 'self_description', 'verified_type', 'refined', 'date_media', 'issue_type_media', 'issue_type_words_media', 'action_form_media', 'action_form_words_media']
print(df_userpolice)

for row in df_userpolice:
  print(row)
for row in df_eventpolice:
  print(row)

df_userpolice.groupby['event_id'].groups.keys() <------this is where the error happens

但我不断收到以下错误:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-38-75f25f3b87eb> in <module>()
     15 # Drop NA values, listing the converted columns explicitly
     16 #   so NA values in other columns aren't dropped
---> 17 df.dropna(subset = ['event_id'])
     18 
     19 

/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py in dropna(self, axis, how, thresh, subset, inplace)
   4746             check = indices == -1
   4747             if check.any():
-> 4748                 raise KeyError(list(np.compress(check, subset)))
   4749             agg_obj = self.take(indices, axis=agg_axis)
   4750 

KeyError: ['event_id']

我真的不知道我哪里错了。

【问题讨论】:

    标签: python pandas dataframe pandas-groupby


    【解决方案1】:

    问题在于“columns['event_id']”。

    这里的列类型是列表,列表项可以通过其索引访问,但不像列['event_id']。

    我希望您在这里的意图是创建一个字典“列”,但您声明“列”的方式是类型列表。

    如果您希望“列”成为字典,请尝试这样的操作。

    columns = {'city_indiv': 'city_name', 'post_id_indiv': 'post_id', 'content_indiv':'content_of_indiv', 'content_media':'content_of_media'}。

    然后你可以对列进行键值操作,因为它是dict类型。

    【讨论】:

      猜你喜欢
      • 2015-12-09
      • 2021-11-28
      • 1970-01-01
      • 1970-01-01
      • 2020-05-14
      • 2018-10-13
      相关资源
      最近更新 更多