Fungsi pandas drop_duplicates() digunakan dalam menganalisis data duplikat dan menghapusnya. Fungsi pada dasarnya membantu dalam menghapus duplikat dari DataFrame. Ini adalah salah satu fungsi umum di perpustakaan Pandas yang merupakan fungsi penting ketika kami mengerjakan kumpulan data dan menganalisis data. Pahami Pandas DataFrame drop_duplicates() Fungsi panda drop_duplicates() mengembalikan DataFrame dengan menghapus baris duplikat. Untuk menghapus baris duplikat dari DataFrame, gunakan Pandas DataFrame drop_duplicates(). Ini memiliki parameter berikut: Fungsi drop_duplicates() mengembalikan DataFrame dengan baris duplikat yang dihapus atau Tidak ada jika di tempat = Benar. Tulis sebuah
program untuk menunjukkan cara kerja drop_duplicates(). Adobe Creative Cloud untuk Tim mulai dari ,99 per bulan. IKLAN MELALUI KARBON import pandas as pd data_dict = {'Name': ['Rohit', 'Karan', 'Shivam', 'Karan'], 'Age': [21, 23, 31, 23],'City':['Patna','Kolkata','Mumbai','Kolkata']} df = pd.DataFrame(data_dict) print(df) df2 = df.drop_duplicates() print('
After removal of duplicate rows:
') print(df2)
Keluaran Name Age City 0 Rohit 21 Patna 1 Karan 23 Kolkata 2 Shivam 31 Mumbai 3 Karan 23 Kolkata After removal of duplicate rows: Name Age City 0 Rohit 21 Patna 1 Karan 23 Kolkata 2 Shivam 31 Mumbai Pada contoh di atas, kita dapat melihat bahwa kita memiliki 2 baris berulang dengan nama siswa sebagai Karan. Karenanya setelah menggunakan drop_duplicates() kami dapat menghapus baris duplikat. Contoh 2: Tulis program untuk menghapus duplikat dari kolom tertentu menggunakan drop_duplicates().Lihat kode berikut. Bisakah kamu melihat twitter tanpa akun? import pandas as pd data_dict = {'Name': ['Rohit', 'Karan', 'Shivam', 'Ajit'], 'Age': [21, 23, 31, 23], 'City': ['Patna', 'Kolkata', 'Mumbai', 'Kolkata']} df = pd.DataFrame(data_dict) print(df) df.drop_duplicates(subset='City', keep=False, inplace=True) print('
DataFrame after removing students belonging to same city:
', df) Keluaran Name Age City 0 Rohit 21 Patna 1 Karan 23 Kolkata 2 Shivam 31 Mumbai 3 Ajit 23 Kolkata DataFrame after removing students belonging to same city: Name Age City 0 Rohit 21 Patna 2 Shivam 31 Mumbai Di sini, dalam contoh di atas, kita dapat melihat bahwa Karan dan Ajit berasal dari kota yang sama Kolkata. Oleh karena itu kami telah menghapus duplikat dengan mempertimbangkan satu kolom yaitu Kota dan mengabaikan semua fakta lainnya. Setelah itu, kami mencetak DataFrame yang dihasilkan. #python #fungsi setel ulang kata sandi email at&t saya appdividend.comUntuk menghapus baris duplikat dari DataFrame, gunakan Pandas DataFrame drop_duplicates(). drop_duplicates() mengembalikan df dengan menghapus baris duplikat. View Discussion Improve Article Save Article View Discussion Improve Article Save Article Pandas drop_duplicates() method helps in removing duplicates from the Pandas Dataframe In Python. Syntax of df.drop_duplicates()
Example:As we can see one of the TeamA and team has been dropped due to duplicate value. Python3
Output: A B C 0 TeamA 50 True 1 TeamB 40 False 3 TeamC 30 False To download the CSV file used, Click Here. Example 1: Removing rows with the same First NameIn the following example, rows having the same First Name are removed and a new data frame is returned.
Python3
Output: As shown in the image, the rows with the same names were removed from a data frame. Example 2: Removing rows with all duplicate valuesIn this example, rows having all values will be removed. Since the CSV file isn’t having such a row, a random row is duplicated and inserted into the data frame first. Python3
Output: As shown in the output image, the length after removing duplicates is 999. Since the keep parameter was set to False, all of the duplicate rows were removed. |