我有一个要添加另一列的数据框,该数据框取决于基于该特定单元格中其他列的值。
我不断收到TypeError: string indices must be integers, not str
。
这是我的数据框:df,其中所有列的值均为字符串格式
ID Key
_1 A
_2 B, C
_3 A
_4 D, E
_5 B, C
我的预期输出是
ID Key Name
_1 A n0, n1
_2 B, C n2
_3 A n3
_4 D, E n4
_5 B, C n5, n6
这是我所做的:
df[df['ID'].str.contains('1')]['Name'] = 'n0, n1' that gave me Type Error.
请注意,id匹配是故意的子字符串匹配。
尝试使用numpy where
,但这也给了我同样的错误。我关注了This link。
基于列值的子集设置新列值的正确方法是什么?另外,我将在以后介绍每个ID
(此处为1到5)中执行的所有值。
答案 0 :(得分:0)
以下对我有用:
df.loc[df['ID'].str.contains('1'), 'Name'] = 'n0, n1'
基本上,您需要使用.loc [row_index,col_index] = val来修改现有数据框。
使用df [row_index] [col_index]只会创建一个我相信的值的副本。
这还假设您已经定义了该列:
df['Name'] = pd.Series()