[英]Pandas: Calculate total percent difference between two data frames
I have two dataframes. 我有两个数据框。 I want to figure out the total percent difference For example:
我想找出总百分比差异例如:
DataFrame1
数据框1
A
一种
11个
22
33
44
DataFrame2
数据框2
B乙
11个
33
33
44
Total percentage the same = 75% 总百分比相同= 75%
Try: 尝试:
df1.eq(df2.values).mean()
Output: 输出:
A 0.75
dtype: float64
from your example DataFrame1 and 2 are df1, df2 here: 在您的示例DataFrame1和2中,此处为df1,df2:
import pandas as pd
df1=pd.DataFrame([1,2,3,4], columns=['A'])
df2=pd.DataFrame([1,3,3,4], columns=['B'])
print('%.f %%' % (100 * (df1.values == df2.values).sum() / df1.size))
it prints 75% 它打印75%
explanation is this: 解释是这样的:
(df1.values == df2.values).sum()
is the number of equal values in the vectors. 是向量中相等值的数量。 So it's 3 in your example
所以在你的例子中是3
and 和
df1.size
is the length of the vector. 是向量的长度。 So 4, in your example
因此,在您的示例中为4
dataframe1 = open("dataframe1.txt")
dataframe2 = open("dataframe2.txt")
data1 = []
data2 = []
counter=0
for i in dataframe1:
data1.append(i)
for i in dataframe2:
data2.append(i)
for i in range(len(data1)):
if data1[i] == data2[i]:
counter += 1
print("Total percentage the same = ",round((counter/len(data1))*100),"%")
You will need to put your data in a txt file as it is named or even chane it if you want. 您将需要将数据放入一个命名为txt的文件中,或者根据需要对其进行处理。 Also you can just type it in in data1 and data2 as you want.
您也可以根据需要在data1和data2中键入它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.