Created
February 26, 2021 07:51
-
-
Save s1rat-dev/38805f3d1456f860fd6ca66f9c0fb4d7 to your computer and use it in GitHub Desktop.
[PYTHON] Pandas DataFrame methodlarının (unique,nunique,sort,pivot_table vs.) incelenmesi.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
data = { | |
'Column1': [1,2,3,4,5,6], | |
'Column2': [10,20,13,20,25,-3], | |
'Column3': ['abc','bca','ade','cba','dea','cbea'] | |
} | |
df = pd.DataFrame(data) | |
result = df | |
result = df['Column2'].unique() | |
# Tekrar etmeyen değerleri ekrana getirir. | |
result = df['Column2'].nunique() | |
# number of unique, 4 tane tekrar etmeyen değer bulur. | |
result = df['Column2'].value_counts() | |
# Elemeanların kaç adet olduğunu yazdırır. | |
''' | |
20 2 | |
13 1 | |
10 1 | |
25 1 | |
''' | |
result = df['Column1'] * 2 | |
# Bu çarpım işlemini başka bir methodla da yapabiliriz. | |
######### | |
# APPLY # | |
######### | |
def karesiniAl(x): | |
return x * x | |
def isItPossitive(x): | |
if x > 0: | |
return True | |
return False | |
def lenString(x): | |
if type(x) == str: | |
return len(x) | |
return x | |
result = df['Column1'].apply(karesiniAl) | |
result = df['Column1'].apply(lambda x:x*x) | |
result = df['Column2'].apply(isItPossitive) | |
result = df['Column2'].apply(lambda x: x > 0) | |
result = df['Column3'].apply(lenString) | |
result = df['Column3'].apply(lambda x : len(x) if(type(x) == str) else x) | |
# Uygulanacak olan fonksiyonu lambda ile de yazabiliriz. | |
df['Column4'] = df['Column3'].apply(lenString) | |
result = df.columns | |
result = df.index | |
result = len(df.columns) | |
result = len(df.index) | |
result = df.info | |
############## | |
# SORT VALUE # | |
############## | |
result = df.sort_values('Column2')['Column2'] | |
''' | |
10 -3 | |
20 10 | |
13 => 13 | |
20 20 | |
25 20 | |
-3 25 | |
''' | |
result = df.sort_values('Column3')['Column3'] | |
''' | |
0 abc | |
2 ade | |
1 bca | |
3 cba | |
5 cbea | |
4 dea | |
(Stringe uygulanmış hali) | |
''' | |
result = df.sort_values('Column1', ascending= False)['Column1'] | |
# Büyükten küçüğe sıralama.. | |
################################################################### | |
data = { | |
'Ay': ['Mayıs','Haziran','Nisan','Mayıs','Haziran','Nisan','Mayıs','Haziran','Nisan',], | |
'Kategori': ['Elektronik','Elektronik','Elektronik','Kitap','Kitap','Kitap','Giyim','Giyim','Giyim'], | |
'Gelir' : [20,30,15,14,32,42,12,36,52] | |
} | |
df = pd.DataFrame(data) | |
result = df | |
''' | |
Ay Gelir Kategori | |
0 Mayıs 20 Elektronik | |
1 Haziran 30 Elektronik | |
2 Nisan 15 Elektronik | |
3 Mayıs 14 Kitap | |
4 Haziran 32 Kitap | |
5 Nisan 42 Kitap | |
6 Mayıs 12 Giyim | |
7 Haziran 36 Giyim | |
8 Nisan 52 Giyim | |
''' | |
# Pivot_table tekrar eden değerleri birleştirmek amacıyla kullanılır, | |
# Çok fazla tekrar eden görüntü yerine, daha sade hal almasını sağlar. | |
result = df.pivot_table(index='Ay',columns='Kategori',values='Gelir') | |
''' | |
Kategori Elektronik Giyim Kitap | |
Ay | |
Haziran 30 36 32 | |
Mayıs 20 12 14 | |
Nisan 15 52 42 | |
''' | |
result = df.pivot_table(index='Kategori',columns='Ay',values='Gelir') | |
''' | |
Ay Haziran Mayıs Nisan | |
Kategori | |
Elektronik 30 20 15 | |
Giyim 36 12 52 | |
Kitap 32 14 42 | |
''' | |
_data = { | |
'Ad-soyad': ['Ahmet Akgün','Fevzi Çakmak','Ahmet Akgün','Fevzi Çakmak','Ahmet Akgün','Fevzi Çakmak'], | |
'Ay': ['Ocak','Ocak','Şubat','Şubat','Mart','Mart'], | |
'Müşteri': ['Haldun Akça','Gülcan Öz','Semiha Başarır','Seyhun Kurt','Sırat Çöp','Emrullah Kaya'] | |
} | |
df = pd.DataFrame(_data) | |
result = df | |
result = df.pivot_table(index='Ad-soyad',columns='Ay',values='Müşteri',aggfunc=np.sum) | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment