December 9, 2024
Mengubah Data Berdasarkan Kondisi

Belajar Data Science – Mengubah Data Berdasarkan Kondisi

Pada artikel ini akan dibahas mengenai cara mengubah data yang telah dimuat ke Pandas DataFrame.

Jupyter Notebook

Open In Colab

Memuat data ke Dataframe Pandas

In [1]:
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/Onestringlab/notebook/main/pokemon_data.csv')

Menampilkan 10 data teratas

In [2]:
df.head(10)
Out[2]:
# Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def Speed Generation Legendary
0 1 Bulbasaur Grass Poison 45 49 49 65 65 45 1 False
1 2 Ivysaur Grass Poison 60 62 63 80 80 60 1 False
2 3 Venusaur Grass Poison 80 82 83 100 100 80 1 False
3 3 VenusaurMega Venusaur Grass Poison 80 100 123 122 120 80 1 False
4 4 Charmander Fire NaN 39 52 43 60 50 65 1 False
5 5 Charmeleon Fire NaN 58 64 58 80 65 80 1 False
6 6 Charizard Fire Flying 78 84 78 109 85 100 1 False
7 6 CharizardMega Charizard X Fire Dragon 78 130 111 130 85 100 1 False
8 6 CharizardMega Charizard Y Fire Flying 78 104 78 159 115 100 1 False
9 7 Squirtle Water NaN 44 48 65 50 64 43 1 False

Mengubah data pada Type 1 dari Fire menjadi Flamer

In [3]:
df.loc[df['Type 1'] == 'Fire', 'Type 1'] = 'Flamer'
df.head(10)
Out[3]:
# Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def Speed Generation Legendary
0 1 Bulbasaur Grass Poison 45 49 49 65 65 45 1 False
1 2 Ivysaur Grass Poison 60 62 63 80 80 60 1 False
2 3 Venusaur Grass Poison 80 82 83 100 100 80 1 False
3 3 VenusaurMega Venusaur Grass Poison 80 100 123 122 120 80 1 False
4 4 Charmander Flamer NaN 39 52 43 60 50 65 1 False
5 5 Charmeleon Flamer NaN 58 64 58 80 65 80 1 False
6 6 Charizard Flamer Flying 78 84 78 109 85 100 1 False
7 6 CharizardMega Charizard X Flamer Dragon 78 130 111 130 85 100 1 False
8 6 CharizardMega Charizard Y Flamer Flying 78 104 78 159 115 100 1 False
9 7 Squirtle Water NaN 44 48 65 50 64 43 1 False

Kembalikan ke data awal

In [4]:
df.loc[df['Type 1'] == 'Flamer', 'Type 1'] = 'Fire'
df.head(10)
Out[4]:
# Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def Speed Generation Legendary
0 1 Bulbasaur Grass Poison 45 49 49 65 65 45 1 False
1 2 Ivysaur Grass Poison 60 62 63 80 80 60 1 False
2 3 Venusaur Grass Poison 80 82 83 100 100 80 1 False
3 3 VenusaurMega Venusaur Grass Poison 80 100 123 122 120 80 1 False
4 4 Charmander Fire NaN 39 52 43 60 50 65 1 False
5 5 Charmeleon Fire NaN 58 64 58 80 65 80 1 False
6 6 Charizard Fire Flying 78 84 78 109 85 100 1 False
7 6 CharizardMega Charizard X Fire Dragon 78 130 111 130 85 100 1 False
8 6 CharizardMega Charizard Y Fire Flying 78 104 78 159 115 100 1 False
9 7 Squirtle Water NaN 44 48 65 50 64 43 1 False

Mengubah data berdasarkan data kolom yang lainnya

In [5]:
df.loc[df['Type 1'] == 'Fire', 'Legendary'] = 'True'
df.head(10)
Out[5]:
# Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def Speed Generation Legendary
0 1 Bulbasaur Grass Poison 45 49 49 65 65 45 1 False
1 2 Ivysaur Grass Poison 60 62 63 80 80 60 1 False
2 3 Venusaur Grass Poison 80 82 83 100 100 80 1 False
3 3 VenusaurMega Venusaur Grass Poison 80 100 123 122 120 80 1 False
4 4 Charmander Fire NaN 39 52 43 60 50 65 1 True
5 5 Charmeleon Fire NaN 58 64 58 80 65 80 1 True
6 6 Charizard Fire Flying 78 84 78 109 85 100 1 True
7 6 CharizardMega Charizard X Fire Dragon 78 130 111 130 85 100 1 True
8 6 CharizardMega Charizard Y Fire Flying 78 104 78 159 115 100 1 True
9 7 Squirtle Water NaN 44 48 65 50 64 43 1 False

Mengubah data beberapa kolom berdasarkan 1 kondisi

In [6]:
df.loc[df['HP'] >= 80, ['Generation','Legendary']] = [2,'True']
df.head(10)
Out[6]:
# Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def Speed Generation Legendary
0 1 Bulbasaur Grass Poison 45 49 49 65 65 45 1 False
1 2 Ivysaur Grass Poison 60 62 63 80 80 60 1 False
2 3 Venusaur Grass Poison 80 82 83 100 100 80 2 True
3 3 VenusaurMega Venusaur Grass Poison 80 100 123 122 120 80 2 True
4 4 Charmander Fire NaN 39 52 43 60 50 65 1 True
5 5 Charmeleon Fire NaN 58 64 58 80 65 80 1 True
6 6 Charizard Fire Flying 78 84 78 109 85 100 1 True
7 6 CharizardMega Charizard X Fire Dragon 78 130 111 130 85 100 1 True
8 6 CharizardMega Charizard Y Fire Flying 78 104 78 159 115 100 1 True
9 7 Squirtle Water NaN 44 48 65 50 64 43 1 False

Mengubah data berdasarkan 2 kondisi

In [7]:
df.loc[(df['HP'] >= 50) & (df['Attack'] >= 60), 'Generation'] = 2
df.head(10)
Out[7]:
# Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def Speed Generation Legendary
0 1 Bulbasaur Grass Poison 45 49 49 65 65 45 1 False
1 2 Ivysaur Grass Poison 60 62 63 80 80 60 2 False
2 3 Venusaur Grass Poison 80 82 83 100 100 80 2 True
3 3 VenusaurMega Venusaur Grass Poison 80 100 123 122 120 80 2 True
4 4 Charmander Fire NaN 39 52 43 60 50 65 1 True
5 5 Charmeleon Fire NaN 58 64 58 80 65 80 2 True
6 6 Charizard Fire Flying 78 84 78 109 85 100 2 True
7 6 CharizardMega Charizard X Fire Dragon 78 130 111 130 85 100 2 True
8 6 CharizardMega Charizard Y Fire Flying 78 104 78 159 115 100 2 True
9 7 Squirtle Water NaN 44 48 65 50 64 43 1 False

Kesimpulan

Pengubahan data dapat dilakukan melalui perintah yang telah disediakan oleh Python pada Pandas DataFrame. Untuk artikel lain terkait dengan data science silahkan lihat kumpulan artikelnya disini.

Rajo Intan

Blogger, pemiliki Onestring Lab, menulis artikel terkait teknologi informasi dan pendidikan. Web Developer, berpengalaman lebih dari 20 tahun mengembangkan berbagai aplikasi dan sistem informasi. Kerjasama kontak di onestringlab@gmail.com atau https://forms.gle/xAGKkpi6B3BzJyzk7

View all posts by Rajo Intan →
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock