Python dataframe正在尝试检索dataframe中的整数
我有一个熊猫数据框,如下所示:Python dataframe正在尝试检索dataframe中的整数,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个熊猫数据框,如下所示: s = index_df[(index_df['id2'].values == result[z][3])] print s.iloc[:, [0]] 这会给我结果 id1 36 14559 我正在尝试将值14559存储到具有以下内容的变量中: value = s.iloc[:, [0]] 但它总是给我一个错误: ValueError: Incompatible indexer with DataFrame 你知道我该怎么解
s = index_df[(index_df['id2'].values == result[z][3])]
print s.iloc[:, [0]]
这会给我结果
id1
36 14559
我正在尝试将值14559存储到具有以下内容的变量中:
value = s.iloc[:, [0]]
但它总是给我一个错误:
ValueError: Incompatible indexer with DataFrame
你知道我该怎么解决这个问题吗
编辑:
index_df = pd.DataFrame({'id2':[1,2,3,2],
'id1':[10,20,30,40]})
print (index_df)
id2 id1
0 1 10
1 2 20
2 3 30
3 2 40
#if possible specify column name with .loc (`id1`)
L = index_df.loc[index_df['id2'].values == 2, 'id1']
#use parameter if not matched condition and returned empty val
#out = next(iter(L), 'no matched value')
print (out)
20
我的数据帧声明如下:
s = index_df[(index_df['id2'].values == result[z][3])]
print s.iloc[:, [0]]
结果:
result=[(fuzz.WRatio(n, n2),n2,sdf.index[x],bdf.index[y])
for y, n2 in enumerate(Col2['CSGNE_NAME']) if fuzz.WRatio(n, n2)>80 and len(n2) >= 2
]
这就是我如何声明和附加到数据帧的方式:
index_df = pd.DataFrame(columns=['id1','id2', 'score'])
index_df = index_df.append({'id1':result[z][2], 'id2':result[z][3], 'score':result[z][0]}, ignore_index=True)
我认为需要:
s.iloc[:, 0]
或:
或将值转换为列表,并使用next
提取第一个值:
L = index_df[(index_df['id2'].values == result[z][3])].values.tolist()
#use parameter if not matched condition and returned empty val
out = next(iter(L), 'no matched value')
样本:
index_df = pd.DataFrame({'id2':[1,2,3,2],
'id1':[10,20,30,40]})
print (index_df)
id2 id1
0 1 10
1 2 20
2 3 30
3 2 40
#if possible specify column name with .loc (`id1`)
L = index_df.loc[index_df['id2'].values == 2, 'id1']
#use parameter if not matched condition and returned empty val
#out = next(iter(L), 'no matched value')
print (out)
20
如果我使用您的代码,我会立即遇到StopIteration,没有任何类型的输出是的,问题是空值,请使用第二个。我现在编辑答案。结果是什么?另一个df?还是什么口述?刚刚编辑了原始帖子,这是两个excel之间的模糊对比files@dythe-所以在循环中调用
result
?是否可能有些值是emty而不可能索引,如[z][2]
?我不容易找到问题,因为无法运行您的解决方案,因为没有数据。有可能创造吗?