我有一个包含许多列的索引数据帧,例如:
Feature1
Feature2
Feature3
Feature4
....
我只想实现一个函数,创建一个新的dataframe(或另一个数据结构类型)对象,如果值相等,它将比较一个测试样本行值和所有其他行(包括测试样本)的值;比较结果将是“1”或“0”,但由于我有91列,我不想引用列名,我见过许多例子,其中一些函数使用列名。你知道吗
分类数据对象的数据示例(NaN表示null
)
_product Feature1 Feature2 Feature3 Feature4
SRI3012 1 yes IN NaN
SRI3015 1 yes IN NaN
SRS3012 1 no OUT Val1
我只是试过:
##Choose sample
test_sample = classified_data.sample();
#Find index of random sample
test_product_code = list(test_sample.index.values)[0]
##Find location of random product in data-set
test_index = classified_data.index.get_loc(test_product_code)
#print(test_sample);
#print(classified_data[(test_index):(test_index+1)])
enum_similarity_data = pandas.DataFrame(calculate_similarity_for_categorical(classified_data[(test_index):(test_index+1)],classified_data).T,index=classified_data.index)
def calculate_similarity_for_categorical(value1,value2):
if(value1 == value2):
return 1;
else:
return 0;
SRI3012(假设随机选择)数据帧或其他具有列名和值的对象的所需输出:
_product Feature1 Feature2 Feature3 Feature4
SRI3012 1 1 1 1
SRI3015 1 1 1 1
SRS3012 1 0 0 0
我不能评论,所以我在这里评论。正如Quang Hoang所评论的,您不应该使用screanshots,而应该使用简单且格式良好的数据,任何花费宝贵时间帮助您的人都可以复制这些数据。而且,所有这些复杂的信息都是不必要的。你可以用一个简单的虚拟数据框和简单的值和名称来重现问题的概念。这样你会得到更好更快的答案。你知道吗
试试这个:
测向:
样品:
结果:
DataFrame.eq
您可以检查一行与指定
axis=1
的所有其他行的相等性。这里的比较应该是DataFrame.eq(Series, axis=1)
如果你认为NaN == NaN
是True
(这不是标准),我们需要单独处理。你知道吗相关问题 更多 >
编程相关推荐