编辑:我在“event\u id”前面添加了“columns”,因为我之前尝试仅使用“event\u id”的代码时一直得到“keyrerror:”event\u id“。我现在去掉了“columns”代码,但仍然得到相同的keyrerror;我检查了“event\u id”,python将其识别为列……有什么建议吗
我正试图根据“事件id”将df_userpolice聚合到类别中,然后聚合每个事件id的所有其他数字(每个事件id的平均跟随者计数,等等);然后我需要将其与较小的数据框df_eventpolice合并。我已在excel中将event_id中的每一行都更改为整数,但由于某些原因,它仍然无法工作,这是我的代码:
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']”
这里的列类型是list,列表项可以通过其索引访问,但与列['event_id']不同
我希望您在这里的意图是创建一个dict“columns”,但是您声明“columns”的方式是类型列表
如果你想让“列”成为一个dict,就试试这样的东西
列={'city_indiv':'city_name','post_id_indiv':'post_id','content_indiv':'content_of indiv','content_media':'content_of the media'}
然后可以对列执行键值操作,因为它是dict类型
相关问题 更多 >
编程相关推荐