一致と不一致について2つのDFを比較する必要があります。また、不一致が発生した場合にマスターdfからの回答を特定する必要があります。

スティーブ

Pythonに2つのデータフレームがあり、2つを比較して、一致と不一致を探します。ただし、不一致の中で、どの回答がマスター回答シートからのもので、どの回答がユーザーの回答からのものであるかを識別できることが重要です。

これを実現するためにpandasdf.where関数を使用することにしました。これは、マスター回答シートからの回答と、不一致が発生した場合のユーザーの回答からの回答を識別する機能を除いて機能しました。

# I have a DataFrame called df_master (master answer sheet)

import pandas as pd

df_master = pd.DataFrame({'B0': [1, 0, 0, 0, 0, 1],
            'B1': [0, 0, 0, 0, 1, 0],
            'B2': [0, 1, 0, 0, 0, 0],
            'B3': [0, 0, 1, 0, 0, 0],
            'B4': [0, 0, 0, 1, 0, 0]})
print(df_master)

#    B0  B1  B2  B3  B4
# 0   1   0   0   0   0
# 1   0   0   1   0   0
# 2   0   0   0   1   0
# 3   0   0   0   0   1
# 4   0   1   0   0   0
# 5   1   0   0   0   0

# I also have a DataFrame called df_answers (users answers)

df_answers = pd.DataFrame({'B0': [0, 0, 0, 0, 0, 1],
            'B1': [1, 0, 0, 0, 1, 0],
            'B2': [0, 0, 0, 0, 0, 0],
            'B3': [0, 1, 1, 0, 0, 0],
            'B4': [0, 0, 0, 1, 0, 0]})

print(df_answers)

#    B0  B1  B2  B3  B4
# 0   0   1   0   0   0
# 1   0   0   0   1   0
# 2   0   0   0   1   0
# 3   0   0   0   0   1
# 4   0   1   0   0   0
# 5   1   0   0   0   0

# when I compare the the two df's, for each match, matches correctly, where there
# is no match I have used other=2.  However this is a problem as I cannot see which is
# the correct answer.  It would be great if there was a way to work the code to reflect
# the master as a 3 and the incorrect answer from the users to stay 2?

comparison = df_master.where(df_master.values==df_answers.values, other=2)

print(comparison)

# My Results

#    B0  B1  B2  B3  B4
# 0   2   2   0   0   0
# 1   0   0   2   2   0
# 2   0   0   0   1   0
# 3   0   0   0   0   1
# 4   0   1   0   0   0
# 5   1   0   0   0   0

# Expected Results

#    B0  B1  B2  B3  B4
# 0   3   2   0   0   0
# 1   0   0   3   2   0
# 2   0   0   0   1   0
# 3   0   0   0   0   1
# 4   0   1   0   0   0
# 5   1   0   0   0   0
BEN_YO

replaceafter str sum、psを使用する場合:次のように自分でマッピングを定義します{'00':'both failed', '01': 'master failed'...}

(df_answers.astype(str)+df_master.astype(str)).replace({'00':0,'01':3,'10':2,'11':1})
Out[129]: 
   B0  B1  B2  B3  B4
0   3   2   0   0   0
1   0   0   3   2   0
2   0   0   0   1   0
3   0   0   0   0   1
4   0   1   0   0   0
5   1   0   0   0   0

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ