简体   繁体   English

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

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

edit: i added the "columns" in front of the "event_id" because i kept getting "keyerror: "event_id" when i tried the code with just 'event_id' alone before. i took the "columns" code off now, but am still getting the same keyerror; i checked the "event_id" and it is recognized as a column by python....any suggestions??编辑:我在“event_id”前面添加了“columns”,因为当我之前仅使用“event_id”尝试代码时,我一直收到“keyerror:“event_id”。我现在去掉了“columns”代码,但是我仍然得到相同的keyerror;我检查了“event_id”,它被python识别为一列……有什么建议吗?

i'm trying to aggregate df_userpolice into categories according to "event_id" and then aggregate all the other numbers for each event_id (average follower count for each event_id, etc.);我正在尝试根据“event_id”将 df_userpolice 汇总到类别中,然后汇总每个 event_id 的所有其他数字(每个 event_id 的平均粉丝数等); then i need to merge it with the smaller dataframe df_eventpolice.然后我需要将它与较小的数据帧 df_eventpolice 合并。 i've changed every row in event_id into integer in excel, but it's still not working for some reason, this is my code:我已将 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

but i keep getting the following error:但我不断收到以下错误:

---------------------------------------------------------------------------
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']

i really don't know where i'm going wrong.我真的不知道我哪里错了。

The problem is with "columns['event_id']".问题在于“列['event_id']”。

Here the columns type is list and the list items can be accessed by its indexes but not like columns['event_id'].这里的列类型是列表,列表项可以通过其索引访问,但不像 columns['event_id']。

I hope your intention here is to create a dict "columns", but the way you declared the "columns" is type list.我希望您在这里的目的是创建一个字典“列”,但您声明“列”的方式是类型列表。

If you want the "columns" to be a dict, just try something like this.如果您希望“列”成为字典,请尝试这样的操作。

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

Then you can perform the key value operations on the columns as it is a dict type.然后您可以对列执行键值操作,因为它是 dict 类型。

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

暂无
暂无

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

相关问题 TypeError:列表索引必须是整数或切片,而不是尝试 plot 时的 str - TypeError : list indices must be integers or slices, not str when trying to plot 检索ID并获取TypeError:列表索引必须是整数或切片,而不是str - Crawling IDs and getting TypeError: list indices must be integers or slices, not str 获取 TypeError:列表索引必须是整数或切片,而不是 str - Getting a TypeError: list indices must be integers or slices, not str 我不断收到此错误:列表索引必须是整数或切片,而不是 str - I keep getting this error: list indices must be integers or slices, not str 试图 append 列出多个项目,但得到 Python 类型错误:列表索引必须是整数或切片,而不是 str - Trying to append multiple items to list but getting Python TypeError: list indices must be integers or slices, not str 类型错误:列表索引必须是整数或切片,而不是 str - TypeError: list indices must be integers or slices, not str “TypeError:list indices必须是整数或切片,而不是str” - “TypeError: list indices must be integers or slices, not str” TypeError:列表索引必须是整数或切片,而不是 str - TypeError: List indices must be integers or slices and not str “TypeError:列表索引必须是整数或切片,而不是 str” - "TypeError: list indices must be integers or slices, not str" TypeError:列表索引必须是整数或切片而不是 str - TypeError: list indices must be integers or slices not str
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM