[英]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.我有一个 pandas 数据框,它是使用 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.
我能够读取数据并制作 dataframe 但经过一些处理后,我的 dataframe 中有许多冗余值,我想删除重复的值。 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.
我用 drop_duplicates() 尝试了一切,但没有一个对我有用。
From your colab, df1
is a copy of another df
, so you can't really change the values of it's columns.在您的 colab 中,
df1
是另一个df
的副本,因此您无法真正更改其列的值。 You should do:你应该做:
df1 = df[['FROM', 'ATTENDANCE']].copy()
df1['FROM'] = df1['FROM'].str.strip()
df2 = df1.drop_duplicates(keep='last')
Output: Output:
FROM ATTENDANCE
2 Usha Dubey PRESENT
9 Pranjal Srivastava PRESENT
11 Jagriti Gupta PRESENT
12 Samaksh X A PRESENT
13 Bhavya Malik PRESENT
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.