简体   繁体   English

我无法在 python 的数据框中删除特定值

[英]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.

相关问题 我无法从 python 中的 JSON 数据中找到属性值 - I am unable to find attribute values from JSON data in python 当我尝试删除所有不以特定名称开头的行时,如何将列名保留在数据框中? - How do I keep the column names in a data frame when I am trying to drop all of the rows that don't start with specific names? 为什么我无法在数据框中添加列名? - Why am I unable to add a column name to a data frame? 如何在熊猫的数据框中删除 * 值? - How do I drop * values in a data frame in pandas? Python/Pandas:如何根据个人 ID 替换 Pandas 数据框的特定值? - Python/Pandas: How do I replace specific values of a Pandas Data Frame based on individual id? 如何使用 python 从 pandas 数据帧中获取特定值? - How to get specific values in from a pandas data frame using python? 仅为某些特定元素向数据框添加值 - python - add values to data frame for only some specific elements - python 使用python更改特定pandas数据框列中的行值 - Change row values in specific pandas data frame column with python 根据最终 excel 文件中特定列中的空/空白值删除一行 - Pandas 数据框 - Drop a row based on empty/blanks values in specific column in final excel file - Pandas Data frame 在Python中使用特定的数据框 - Using specific data frame in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM