pandas - KeyError : "None of [Index([. ..] 在 [列] 中

标签 pandas

我有形状为 (3, 50) 的 numpy 数组:

data = np.array([[0, 3, 0, 2, 0, 0, 1, 2, 2, 0, 1, 0, 0, 0, 0, 0, 0, 2, 1, 2, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 1, 0, 0, 7, 0, 0, 0, 0,
        1, 1, 2, 0, 0, 2],
       [0, 0, 0, 0, 0, 3, 0, 1, 6, 1, 1, 0, 0, 0, 0, 2, 0, 0, 1, 0, 1, 0,
        3, 0, 0, 0, 0, 0, 0, 5, 2, 2, 2, 1, 0, 0, 1, 0, 1, 3, 2, 0, 0, 0,
        0, 0, 2, 0, 0, 0],
       [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0, 1, 0,
        0, 0, 0, 0, 0, 0]])

以及以下列名称:

new_cols = [f'description_word_{i+1}_count' for i in range(50)]

我正在尝试以这种方式在现有数据框中添加新列:

df[new_cols] = data

但出现错误:

KeyError: "None of [Index(['description_word_1_count', 'description_word_2_count',\n 'description_word_3_count', 'description_word_4_count',\n 'description_word_5_count', 'description_word_6_count',\n 'description_word_7_count', 'description_word_8_count',\n 'description_word_9_count', 'description_word_10_count',\n 'description_word_11_count', 'description_word_12_count',\n 'description_word_13_count', 'description_word_14_count',\n 'description_word_15_count', 'description_word_16_count',\n 'description_word_17_count', 'description_word_18_count',\n 'description_word_19_count', 'description_word_20_count',\n 'description_word_21_count', 'description_word_22_count',\n 'description_word_23_count', 'description_word_24_count',\n 'description_word_25_count', 'description_word_26_count',\n 'description_word_27_count', 'description_word_28_count',\n 'description_word_29_count', 'description_word_30_count',\n 'description_word_31_count', 'description_word_32_count',\n 'description_word_33_count', 'description_word_34_count',\n 'description_word_35_count', 'description_word_36_count',\n 'description_word_37_count', 'description_word_38_count',\n 'description_word_39_count', 'description_word_40_count',\n 'description_word_41_count', 'description_word_42_count',\n 'description_word_43_count', 'description_word_44_count',\n 'description_word_45_count', 'description_word_46_count',\n 'description_word_47_count', 'description_word_48_count',\n 'description_word_49_count', 'description_word_50_count'],\n dtype='object')] are in the [columns]"

而且我不知道它在哪里找到我的列名称中的“\n”符号。

同时用数据创建一个新的数据框就可以了:

new_df = pd.DataFrame(data=data, columns=new_cols)

有谁知道这个错误是什么原因造成的吗?

最佳答案

假设你有一个像这样的 df:

df = pd.DataFrame({'person': [1,1,1], 'event': ['A','B','C']})

您可以像这样添加新列:

import pandas as pd
import numpy as np


data = np.array([[0, 3, 0, 2, 0, 0, 1, 2, 2, 0, 1, 0, 0, 0, 0, 0, 0, 2, 1, 2, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 1, 0, 0, 7, 0, 0, 0, 0,
        1, 1, 2, 0, 0, 2],
       [0, 0, 0, 0, 0, 3, 0, 1, 6, 1, 1, 0, 0, 0, 0, 2, 0, 0, 1, 0, 1, 0,
        3, 0, 0, 0, 0, 0, 0, 5, 2, 2, 2, 1, 0, 0, 1, 0, 1, 3, 2, 0, 0, 0,
        0, 0, 2, 0, 0, 0],
       [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0, 1, 0,
        0, 0, 0, 0, 0, 0]])

new_cols = [f'description_word_{i+1}_count' for i in range(50)]

df[new_cols] = pd.DataFrame(data, index=df.index)

我认为问题在于,当您实际上需要创建多个系列时,您正在使用语法来创建系列。换句话说,一个数据框。

关于pandas - KeyError : "None of [Index([. ..] 在 [列] 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64197239/

相关文章:

python - 具有相同颜色和标记的散点图子图

python - 在各自的单热编码列中填充分类数据的值

python - 当 NaN 包含在索引中时,如何有效地删除?

python - Pandas 在完全匹配和部分匹配上合并 DataFrame

python - 如何将 Pandas 数据框中的列拆分为字母值和数值?

python - 将 1 天添加到日期列的选定时间段

python - 将列从一个数据框映射到另一个数据框以创建新列

python - Pandas 分组 : TOP 3 values for each group

python - 如何在一行中计算数据框中的并发事件?

python - 省时的宽到长转换 Pandas