November 30, 2021
Mengurutkan dan Menambahkan Kolom Pada Dataframe

Mengurutkan dan Menambahkan Kolom Pada Dataframe

Pada artikel ini akan dibahas cara mengurutkan data pada data yang telah dimuat ke dalam Pandas DataFrame . Selain itu, akan dibahas juga mengenai cara menambahkan kolom pada data tersebut.

Jupyter Notebook

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')

Deskripsi data

In [2]:
df.describe()
Out[2]:
# HP Attack Defense Sp. Atk Sp. Def Speed Generation
count 800.000000 800.000000 800.000000 800.000000 800.000000 800.000000 800.000000 800.00000
mean 362.813750 69.258750 79.001250 73.842500 72.820000 71.902500 68.277500 3.32375
std 208.343798 25.534669 32.457366 31.183501 32.722294 27.828916 29.060474 1.66129
min 1.000000 1.000000 5.000000 5.000000 10.000000 20.000000 5.000000 1.00000
25% 184.750000 50.000000 55.000000 50.000000 49.750000 50.000000 45.000000 2.00000
50% 364.500000 65.000000 75.000000 70.000000 65.000000 70.000000 65.000000 3.00000
75% 539.250000 80.000000 100.000000 90.000000 95.000000 90.000000 90.000000 5.00000
max 721.000000 255.000000 190.000000 230.000000 194.000000 230.000000 180.000000 6.00000

Mengurutkan data

In [3]:
df.sort_values('Name',ascending=True)
Out[3]:
# Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def Speed Generation Legendary
510 460 Abomasnow Grass Ice 90 92 75 92 85 60 4 False
511 460 AbomasnowMega Abomasnow Grass Ice 90 132 105 132 105 30 4 False
68 63 Abra Psychic NaN 25 20 15 105 55 90 1 False
392 359 Absol Dark NaN 65 130 60 75 60 75 3 False
393 359 AbsolMega Absol Dark NaN 65 150 60 115 60 115 3 False
... ... ... ... ... ... ... ... ... ... ... ... ...
632 571 Zoroark Dark NaN 60 105 60 120 60 105 5 False
631 570 Zorua Dark NaN 40 65 40 80 40 65 5 False
46 41 Zubat Poison Flying 40 45 35 30 40 55 1 False
695 634 Zweilous Dark Dragon 72 85 70 65 70 58 5 False
794 718 Zygarde50% Forme Dragon Ground 108 100 121 81 95 95 6 True

800 rows × 12 columns

In [4]:
df.sort_values(['Type 1','HP'],ascending=[True,False])
Out[4]:
# Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def Speed Generation Legendary
520 469 Yanmega Bug Flying 86 76 86 116 56 95 4 False
698 637 Volcarona Bug Fire 85 60 65 135 105 100 5 False
231 214 Heracross Bug Fighting 80 125 75 40 95 85 2 False
232 214 HeracrossMega Heracross Bug Fighting 80 185 115 40 105 75 2 False
678 617 Accelgor Bug NaN 80 70 40 100 60 145 5 False
... ... ... ... ... ... ... ... ... ... ... ... ...
106 98 Krabby Water NaN 30 105 90 25 25 50 1 False
125 116 Horsea Water NaN 30 40 70 70 25 60 1 False
129 120 Staryu Water NaN 30 45 55 70 55 85 1 False
139 129 Magikarp Water NaN 20 10 55 15 20 80 1 False
381 349 Feebas Water NaN 20 15 20 10 55 80 3 False

800 rows × 12 columns

Menambahkan kolom Total

In [5]:
df['Total'] = df['HP'] + df['Attack'] + df['Defense'] + df['Sp. Atk'] + df['Sp. Def'] + df['Speed']
df.head(5)
Out[5]:
# Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def Speed Generation Legendary Total
0 1 Bulbasaur Grass Poison 45 49 49 65 65 45 1 False 318
1 2 Ivysaur Grass Poison 60 62 63 80 80 60 1 False 405
2 3 Venusaur Grass Poison 80 82 83 100 100 80 1 False 525
3 3 VenusaurMega Venusaur Grass Poison 80 100 123 122 120 80 1 False 625
4 4 Charmander Fire NaN 39 52 43 60 50 65 1 False 309

Menghapus kolom Total

In [6]:
df = df.drop(columns = ['Total'])
df.head()
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 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

Menambahkan kolom Total

In [7]:
df['Total'] = df.iloc[:,4:10].sum(axis=1)
df.head()
Out[7]:
# Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def Speed Generation Legendary Total
0 1 Bulbasaur Grass Poison 45 49 49 65 65 45 1 False 318
1 2 Ivysaur Grass Poison 60 62 63 80 80 60 1 False 405
2 3 Venusaur Grass Poison 80 82 83 100 100 80 1 False 525
3 3 VenusaurMega Venusaur Grass Poison 80 100 123 122 120 80 1 False 625
4 4 Charmander Fire NaN 39 52 43 60 50 65 1 False 309

Mengubah urutan kolom

In [8]:
cols = list(df.columns)

df = df[cols[0:10] + [cols[-1]] + cols[10:12]]
df.head(5)
Out[8]:
# Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def Speed Total Generation Legendary
0 1 Bulbasaur Grass Poison 45 49 49 65 65 45 318 1 False
1 2 Ivysaur Grass Poison 60 62 63 80 80 60 405 1 False
2 3 Venusaur Grass Poison 80 82 83 100 100 80 525 1 False
3 3 VenusaurMega Venusaur Grass Poison 80 100 123 122 120 80 625 1 False
4 4 Charmander Fire NaN 39 52 43 60 50 65 309 1 False

Kesimpulan

Cara mengurutkan data dan menambahkan kolom pada Pandas DataFrame mudah untuk dilakukan. 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 →