Comparing Boolean Values of Pandas Dataframes- Returning String

♀尐吖头ヾ 提交于 2021-01-28 04:49:02

问题


I have 4 dataframes I'm going to be comparing, that each look like

ID    Jan    Feb    Mar
1     True   True   False
2     True   True   True
3     False  False  False

anywhere from 2 to 3000 rows. They will have the exact same column names but may not always share all the same index IDs.

What I would like to do is compare them and generate a new dataframe based on their values. For any cell that was False in at least one dataframe, I want to assign it a string (e.g. "False in Dataframe1") and if multiple, append both (e.g. "False in Dataframe1, Dataframe2").

Output would look like

ID    Jan            Feb              Mar
1     True           True             False in A, B, C
2     True           False in B       True
3     False in A     False in A, B    False in A

Is there some kind of direct dataframe to dataframe comparison I can use? Or do I need to concat the dataframes so I can compare the columns to each other?

EDIT- I do not want row-wise comparison, but rather based off of the index, for circumstances where one dataframes does not have the same records.


回答1:


Very close, what you want:

import pandas as pd
import numpy as np
import io

#testing df1,df2,df3
temp=u"""ID,Jan,Feb,Mar
1,True,True,False
2,True,True,True
3,False,False,False"""
df3 = pd.read_csv(io.StringIO(temp), sep=",", index_col=[0])
print df3
temp1=u"""ID,Jan,Feb,Mar
1,True,False,False
2,False,True,True
3,False,True,True"""
df1 = pd.read_csv(io.StringIO(temp1), sep=",", index_col=[0])
print df1
temp2=u"""ID,Jan,Feb,Mar
1,False,False,False
2,False,False,True
3,False,True,True"""
df2 = pd.read_csv(io.StringIO(temp2), sep=",", index_col=[0])
print df2

#concat all dataframes by columns
pieces = {'df1': df1, 'df2': df2, 'df3': df3}
df = pd.concat(pieces, axis=1)
print df

#create new dataframe with size as df filled by column names
levels = df.columns.levels
labels = df.columns.labels
xyz = pd.DataFrame( np.array(levels[0][labels[0]].tolist()*len(df.index)).reshape((len(df.index), len(df.index)*len(pieces))), index=df.index, columns = df.columns)
print xyz

#reset multicolumn to column
xyz.columns = levels[1][labels[1]]
df.columns = levels[1][labels[1]]

#use df as mask - output names of df with False
print xyz.mask(df)

#use df as mask - output names of df with True
out_false =  xyz.mask(df)
print out_false

out_true =  xyz.mask(~df)
print out_true

#create output empty df - for False and for True values
result_false = result_true = pd.DataFrame(index = out_false.index)

#group output dataframe by columns and create new df from series - for False and for True values
for name, group in out_false.groupby(level=0, axis=1):
    #print name
    series = pd.Series(group.apply(lambda x: ','.join(map(str, x.dropna())), axis=1), name=name)
    print
    print series
    result_false = pd.concat([result_false, series], axis=1) 
print result_false   
#        Feb          Jan          Mar
#ID                                   
#1   df1,df2          df2  df1,df2,df3
#2       df2      df1,df2             
#3       df3  df1,df2,df3          df3 

for name, group in out_true.groupby(level=0, axis=1):
    #print name
    series = pd.Series(group.apply(lambda x: ','.join(map(str, x.dropna())), axis=1), name=name)
    result_true = pd.concat([result_true, series], axis=1) 
print result_true  
#        Feb      Jan          Mar
#ID                               
#1       df3  df1,df3             
#2   df1,df3      df3  df1,df2,df3
#3   df1,df2               df1,df2


来源:https://stackoverflow.com/questions/33905944/comparing-boolean-values-of-pandas-dataframes-returning-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!