Python 比较数据帧的布尔值-返回字符串
我将比较4个数据帧,每个数据帧看起来像Python 比较数据帧的布尔值-返回字符串,python,pandas,boolean,dataframe,Python,Pandas,Boolean,Dataframe,我将比较4个数据帧,每个数据帧看起来像 ID Jan Feb Mar 1 True True False 2 True True True 3 False False False 从2到3000行的任意位置。它们将具有完全相同的列名,但可能并不总是共享所有相同的索引ID 我想做的是比较它们,并根据它们的值生成一个新的数据帧。对于在至少一个数据帧中为False的任何单元格,我希望为其分配一个字符串(例如“在Dataframe1中为Fa
ID Jan Feb Mar
1 True True False
2 True True True
3 False False False
从2到3000行的任意位置。它们将具有完全相同的列名,但可能并不总是共享所有相同的索引ID
我想做的是比较它们,并根据它们的值生成一个新的数据帧。对于在至少一个数据帧中为False的任何单元格,我希望为其分配一个字符串(例如“在Dataframe1中为False”),如果有多个,则附加这两个字符串(例如“在Dataframe1中为False,在Dataframe2中为False”)
输出看起来像
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
我是否可以使用某种直接的数据帧到数据帧的比较?或者我是否需要对数据帧进行压缩,以便可以相互比较列
编辑-我不希望按行进行比较,而是基于索引,用于一个数据帧不具有相同记录的情况。非常接近,您需要:
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
您的意思是即使索引不同,也要按行进行比较吗?如果是这样,您需要保证每个数据帧中的行数相同,并重置index@vmg不,不是按排的。基于索引。我将更新以澄清,谢谢。您好,我将按照您的答案解决这类问题,但我在定义xyz方面有问题。即使我用0填充NaN使其变为相等,我也无法重塑。你对此有什么建议吗?