[英]Python comparing variables in 2 dataframe
I am trying to compare the values of 2 data frame. 我正在尝试比较2个数据框的值。 If any of the values is different, I will return that row.
如果任何值不同,我将返回该行。
df1
Value1 Value2
Name
a 1 1
b 1 2
c 0 1
df2
Value Value2
Name
a 1 1
b 1 1
c 1 1
I did a df1==df2 我做了df1 == df2
df3
Value Value2
Name
a True True
b True False
c False True
I want to return only b and c, how can I do it? 我只想返回b和c,该怎么办? I do not want to do
我不想做
df3[(df3['Value']==False)|(df3['Value2'==False)]
because I may have more than 2 columns and columns names can differ 因为我可能有两个以上的列,并且列名称可能不同
Assuming you have a data file (d3.txt) or list of data like (line), 假设您有一个数据文件(d3.txt)或(行)之类的数据列表,
line = [i.strip().split() for i in open("d3.txt").readlines()]
print line
[['#df3'], ['#', 'Value', 'Value2'], ['#Name'], ['#a', 'True', 'True'], ['#b', 'True', 'False'], ['#c', 'False', 'True']]
for i in line[:][:]:
mydict[i[0]] = ",".join(li[li.index(i)][1:])
I just created a dictionary. 我刚刚创建了字典。 So you can call
所以你可以打电话
print mydict
print mydict['#a'] #Depend of which name you want to look.
The output is 输出是
{'#': 'Value,Value2', '#c': 'False,True', '#b': 'True,False', '#a': 'True,True', '#Name': '', '#df3': ''}
True,True
Or you can do this way without creating the dictionary, 或者您可以不创建字典就这样做,
for n in range(len(line)):
if (line[n][0] == '#c' or line[n][0]== '#b'):
print line[n][:]
And the output is (maybe this is what you want): 输出是(也许这就是您想要的):
['#b', 'True', 'False']
['#c', 'False', 'True']
我认为这应该做到:
df3[~df3.all(axis=1)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.