pandas 如何停止获取ValueError:无法使用多维键进行索引

toe95027  于 2023-06-28  发布在  其他
关注(0)|答案(1)|浏览(121)

我正在尝试将每一行数据附加到我的课程的数据框中。我进去了

search_word = "liberty"
results_data = []

for key in text_dictionary:
    word_counts = Counter(text_dictionary[key].split())
    search_count = word_counts[search_word]
    file_names = key
    main_author = ecco_metadata_w_counts.loc[ecco_metadata_w_counts["DocName"] == key]["Author 1"].values[0]
    title = ecco_metadata_w_counts.loc[ecco_metadata_w_counts["DocName"] == key]["Title"].values[0]
    results = {'file_names':file_names,'Author 1':main_author,'title':title,'search_word':search_word,'search_count':search_count}
    results_data.append(results)
results_df = pd.DataFrame(results_data)

我不断得到结果

ValueError: Cannot index with multidimensional key

这个错误是什么意思,我该如何修复?这是整个错误报告。

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-119-d60594554fc3> in <module>
      6     search_count = word_counts[search_word]
      7     file_names = key
----> 8     main_author = ecco_metadata_w_counts.loc[ecco_metadata_w_counts["DocName"] == key]["Author 1"].values[0]
      9     title = ecco_metadata_w_counts.loc[ecco_metadata_w_counts["DocName"] == key]["Title"].values[0]
     10     results = {'file_names':file_names,'Author 1':main_author,'title':title,'search_word':search_word,'search_count':search_count}

~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py in __getitem__(self, key)
    893 
    894             maybe_callable = com.apply_if_callable(key, self.obj)
--> 895             return self._getitem_axis(maybe_callable, axis=axis)
    896 
    897     def _is_scalar_access(self, key: Tuple):

~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexing.py in _getitem_axis(self, key, axis)
   1109 
   1110                 if hasattr(key, "ndim") and key.ndim > 1:
-> 1111                     raise ValueError("Cannot index with multidimensional key")
   1112 
   1113                 return self._getitem_iterable(key, axis=axis)

ValueError: Cannot index with multidimensional key

ecco_metadata_w_counts是这样的:ecco_metadata_w_counts 2nd halfecco_metadata_w_counts 1st half

nkhmeac6

nkhmeac61#

我找到了答案。它有良好的互联网。我住在一个网络不好的地方,第二次我去了一个网络很好的地方,一切都正常。很抱歉浪费了大家的时间。

相关问题