繁体   English   中英

尝试使用 groupby() 函数但不断收到“类型错误:列表索引必须是整数或切片,而不是 str”

[英]trying to use groupby() function but keep getting "TypeError: list indices must be integers or slices, not str"

编辑:我在“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']

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

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

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

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

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

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

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

使用df_userpolice.groupby("event_id").size()获取每个event_id的行数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM