简体   繁体   中英

What is the best way to sort data from one data frame based on another data frame?

I have 2 datasets and I am trying to sort data from one dataset based on the values of the second dataset and create a new column in the first dataset. If the values in dataset 1 and 2 are matching then I want to populate the new column as true else false. What is the best way of doing this in Python? May code (given below) is not working.

Data:

df1
    Index   ID  type 1  type2
0   1   A.34    6.3 7.1
1   2   A.35    5.8 7.3
2   3   A.36    6.8 5.2
3   4   A.37    7.8 6.4
4   5   A.38    6.9 8.8


df2
    Index   ID  Type 2
0   1   A.55    6.7
1   2   A.35    3.6
2   3   A.69    5.8
3   4   A.34    9.2
4   5   A.38    7.7

# Required Output
df3
Index   ID  type 1  type2   Status
0   1   A.34    6.3 7.1 bad
1   2   A.35    5.8 7.3 good
2   3   A.36    4.1 2.6 bad
3   4   A.37    7.8 6.4 bad
4   5   A.38    6.9 8.8 good


# The code I wrote is giving me ‘bad’ for all the rows: 

Boolean = []
for x in df1.ID:
    if x == x in df2.ID:
        Boolean.append('good')
    else:
        Boolean.append('bad')
print (Boolean)

# Output obtained with code
Output: 
['bad', 'bad', 'bad', 'bad', 'bad']

Thank you.

I think this is what you are looking for:

import pandas as pd

data1 = {
    'Index': [1, 2, 3, 4, 5],
    'ID': ['A.34', 'A.35', 'A.36', 'A.37', 'A.38'],
    'type 1': [6.3, 5.8, 6.8, 7.8, 6.9],
    'type2': [7.1, 7.3, 5.2, 6.4, 8.8]}

data2 = {
    'Index': [1, 2, 3, 4, 5],
    'ID': ['A.55', 'A.35', 'A.69', 'A.34', 'A.38'],
    'Type 2': [6.7, 3.6, 5.8, 9.2, 7.7]}

df1 = pd.DataFrame(data=data1)
df2 = pd.DataFrame(data=data2)

merge_cols = ['Index', 'ID']

df = pd.merge(df1, df2[merge_cols], how='left', left_on=merge_cols, right_on=merge_cols, indicator=True)

d = {'left_only':'bad', 'both':'good'}
df['_merge'] = df['_merge'].map(d)
df.rename(columns={'_merge': 'Status'}, inplace=True)
df

The output df looks like this:

    Index   ID  type 1  type2   Status
0   1   A.34    6.3 7.1 bad
1   2   A.35    5.8 7.3 good
2   3   A.36    6.8 5.2 bad
3   4   A.37    7.8 6.4 bad
4   5   A.38    6.9 8.8 good

EDIT: edited to merge on both columns Index and ID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM