简体   繁体   中英

I am unable to drop specific values in a data frame in python

I have a pandas data frame which I made using an text file in Python. I was able to read the data and made the dataframe but after some processing, I am having many redundant values in my dataframe and I want to remove the repeated values. I tried using

df2 = df1.drop_duplicates(subset=['FROM', 'ATTENDANCE'], keep = 'last', inplace=False)
df2

在此处输入图像描述

but still, the repeated data is there and is not removed. I tried everything with drop_duplicates() and nothing of them worked for me.

From your colab, df1 is a copy of another df , so you can't really change the values of it's columns. You should do:

df1 = df[['FROM', 'ATTENDANCE']].copy()
df1['FROM'] = df1['FROM'].str.strip()

df2 = df1.drop_duplicates(keep='last')

Output:

                  FROM ATTENDANCE
2           Usha Dubey    PRESENT
9   Pranjal Srivastava    PRESENT
11       Jagriti Gupta    PRESENT
12         Samaksh X A    PRESENT
13        Bhavya Malik    PRESENT

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