Data frame question. Delete some row as a condition between two associated data frames.

Asked 2 weeks ago, Updated 2 weeks ago, 1 views

Assuming that there are two models like this,

If there is a name of withdrawal in model 2, the subject (national history, physical education) is

I'd like to delete the Korean history of model 1 and row about physical education, but I don't know how to make logic, so I'm asking you a question.

I'm not sure if the logic above is correct, so I'm asking because I don't have a clue.

dataframe

2022-09-20 10:43

1 Answers

You just have to do it step by step by step.

>>> import pandas as pd
>> df1 = pd.DataFrame ({"subject": ["Korean", "Mathematics", "English", "Physical Education", "National History", "Score": [90, 90, 85, 75, 100]})
>>> df2 = pd.DataFrame ({"subject": ["Korean history", "Korean language", "Mathematics", "Sports"], "Name": ["Cheolsoo", "Minsu", "Younghee", "Cheolsoo"})
>>> df1
   a subject score
0 Korean 90
1 Math 90
2 English 85
3 P.E. 75
History of the Four Kingdoms
>>> df2
   Subject Name
the withdrawal of state affairs
Korean Minsu
2 Math Younghee
3 Withdrawal of Physical Education

Only the withdrawal subjects are selected from df2.

>>> df2_withdrawal = df2["name"] == "withdrawal"]
>>> df2_Withdrawal
   Subject Name
the withdrawal of state affairs
3 Withdrawal of Physical Education
>>> df2_Withdrawal ["Subject"]
the history of the nation
3 Physical Education
Name: Subject, dtype: object

The subject column of df1 selects only rows that are not included in the withdrawal subjects selected above. Use the isin and not operators ~.

>>> df1_Only those that are not subject to withdrawal = df1[~df1["subject"].isin(df2_subject"])]
>>> df1_Only those that are not withdrawn
   a subject score
0 Korean 90
1 Math 90
2 English 85


2022-09-20 10:43

If you have any answers or tips


© 2022 pinfo. All rights reserved.