Python Pandas - Temel İşlevsellik
Şimdiye kadar, üç Pandas Veri Yapısını ve bunların nasıl oluşturulacağını öğrendik. Gerçek zamanlı veri işlemedeki önemi nedeniyle büyük ölçüde DataFrame nesnelerine odaklanacağız ve ayrıca diğer birkaç DataStructures'ı tartışacağız.
Seri Temel İşlevsellik
| Sr.No. | Nitelik veya Yöntem ve Açıklama | 
|---|---|
| 1 | axes Satır ekseni etiketlerinin bir listesini verir | 
| 2 | dtype Nesnenin dtype değerini verir. | 
| 3 | empty Seri boşsa True döndürür. | 
| 4 | ndim Tanım 1'e göre temel alınan verilerin boyutlarının sayısını verir. | 
| 5 | size Temel verilerdeki öğelerin sayısını döndürür. | 
| 6 | values Seriyi ndarray olarak döndürür. | 
| 7 | head() İlk n satırı döndürür. | 
| 8 | tail() Son n satırı döndürür. | 
Şimdi bir Seri oluşturalım ve yukarıdaki tablodaki tüm öznitelik işlemlerini görelim.
Misal
import pandas as pd
import numpy as np
#Create a series with 100 random numbers
s = pd.Series(np.random.randn(4))
print sOnun output aşağıdaki gibidir -
0   0.967853
1  -0.148368
2  -1.395906
3  -1.758394
dtype: float64eksenler
Serinin etiketlerinin listesini döndürür.
import pandas as pd
import numpy as np
#Create a series with 100 random numbers
s = pd.Series(np.random.randn(4))
print ("The axes are:")
print s.axesOnun output aşağıdaki gibidir -
The axes are:
[RangeIndex(start=0, stop=4, step=1)]Yukarıdaki sonuç, 0'dan 5'e kadar olan değerler listesinin kompakt bir biçimidir, yani [0,1,2,3,4].
boş
Object'in boş olup olmadığını belirten Boolean değerini döndürür. True, nesnenin boş olduğunu gösterir.
import pandas as pd
import numpy as np
#Create a series with 100 random numbers
s = pd.Series(np.random.randn(4))
print ("Is the Object empty?")
print s.emptyOnun output aşağıdaki gibidir -
Is the Object empty?
Falsendim
Nesnenin boyutlarının sayısını verir. Tanım olarak, bir Seri 1B veri yapısıdır, bu nedenle
import pandas as pd
import numpy as np
#Create a series with 4 random numbers
s = pd.Series(np.random.randn(4))
print s
print ("The dimensions of the object:")
print s.ndimOnun output aşağıdaki gibidir -
0   0.175898
1   0.166197
2  -0.609712
3  -1.377000
dtype: float64
The dimensions of the object:
1boyut
Serinin boyutunu (uzunluğunu) verir.
import pandas as pd
import numpy as np
#Create a series with 4 random numbers
s = pd.Series(np.random.randn(2))
print s
print ("The size of the object:")
print s.sizeOnun output aşağıdaki gibidir -
0   3.078058
1  -1.207803
dtype: float64
The size of the object:
2değerler
Dizideki gerçek verileri bir dizi olarak döndürür.
import pandas as pd
import numpy as np
#Create a series with 4 random numbers
s = pd.Series(np.random.randn(4))
print s
print ("The actual data series is:")
print s.valuesOnun output aşağıdaki gibidir -
0   1.787373
1  -0.605159
2   0.180477
3  -0.140922
dtype: float64
The actual data series is:
[ 1.78737302 -0.60515881 0.18047664 -0.1409218 ]Kuyruk
Bir Series veya DataFrame nesnesinin küçük bir örneğini görüntülemek için head () ve tail () yöntemlerini kullanın.
head() ilkini döndürür nsatırlar (dizin değerlerine dikkat edin). Görüntülenecek varsayılan öğe sayısı beştir, ancak özel bir sayı iletebilirsiniz.
import pandas as pd
import numpy as np
#Create a series with 4 random numbers
s = pd.Series(np.random.randn(4))
print ("The original series is:")
print s
print ("The first two rows of the data series:")
print s.head(2)Onun output aşağıdaki gibidir -
The original series is:
0   0.720876
1  -0.765898
2   0.479221
3  -0.139547
dtype: float64
The first two rows of the data series:
0   0.720876
1  -0.765898
dtype: float64tail() sonuncuyu döndürür nsatırlar (dizin değerlerine dikkat edin). Görüntülenecek varsayılan öğe sayısı beştir, ancak özel bir sayı iletebilirsiniz.
import pandas as pd
import numpy as np
#Create a series with 4 random numbers
s = pd.Series(np.random.randn(4))
print ("The original series is:")
print s
print ("The last two rows of the data series:")
print s.tail(2)Onun output aşağıdaki gibidir -
The original series is:
0 -0.655091
1 -0.881407
2 -0.608592
3 -2.341413
dtype: float64
The last two rows of the data series:
2 -0.608592
3 -2.341413
dtype: float64DataFrame Temel İşlevselliği
Şimdi DataFrame Temel İşlevselliğinin ne olduğunu anlayalım. Aşağıdaki tablolar, DataFrame Temel İşlevselliğine yardımcı olan önemli öznitelikleri veya yöntemleri listeler.
| Sr.No. | Nitelik veya Yöntem ve Açıklama | 
|---|---|
| 1 | T Satırları ve sütunları ters çevirir. | 
| 2 | axes Tek üye olarak satır ekseni etiketleri ve sütun ekseni etiketleri içeren bir liste döndürür. | 
| 3 | dtypes Bu nesnedeki dtype'ları döndürür. | 
| 4 | empty NDFrame tamamen boşsa doğrudur [öğe yok]; eksenlerden herhangi biri 0 uzunluğundaysa. | 
| 5 | ndim Eksen sayısı / dizi boyutları. | 
| 6 | shape DataFrame'in boyutluluğunu temsil eden bir demet döndürür. | 
| 7 | size NDFrame'deki öğelerin sayısı. | 
| 8 | values NDFrame'in numpy gösterimi. | 
| 9 | head() İlk n satırı döndürür. | 
| 10 | tail() Son n satırı döndürür. | 
Şimdi bir DataFrame oluşturalım ve yukarıda bahsedilen tüm özelliklerin nasıl çalıştığını görelim.
Misal
import pandas as pd
import numpy as np
#Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack']),
   'Age':pd.Series([25,26,25,23,30,29,23]),
   'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#Create a DataFrame
df = pd.DataFrame(d)
print ("Our data series is:")
print dfOnun output aşağıdaki gibidir -
Our data series is:
    Age   Name    Rating
0   25    Tom     4.23
1   26    James   3.24
2   25    Ricky   3.98
3   23    Vin     2.56
4   30    Steve   3.20
5   29    Smith   4.60
6   23    Jack    3.80T (Transpoze)
DataFrame'in devrikini döndürür. Satırlar ve sütunlar birbirinin yerine geçecektir.
import pandas as pd
import numpy as np
 
# Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack']),
   'Age':pd.Series([25,26,25,23,30,29,23]),
   'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
# Create a DataFrame
df = pd.DataFrame(d)
print ("The transpose of the data series is:")
print df.TOnun output aşağıdaki gibidir -
The transpose of the data series is:
         0     1       2      3      4      5       6
Age      25    26      25     23     30     29      23
Name     Tom   James   Ricky  Vin    Steve  Smith   Jack
Rating   4.23  3.24    3.98   2.56   3.2    4.6     3.8eksenler
Satır ekseni etiketlerinin ve sütun ekseni etiketlerinin listesini döndürür.
import pandas as pd
import numpy as np
#Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack']),
   'Age':pd.Series([25,26,25,23,30,29,23]),
   'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#Create a DataFrame
df = pd.DataFrame(d)
print ("Row axis labels and column axis labels are:")
print df.axesOnun output aşağıdaki gibidir -
Row axis labels and column axis labels are:
[RangeIndex(start=0, stop=7, step=1), Index([u'Age', u'Name', u'Rating'],
dtype='object')]tipler
Her sütunun veri türünü döndürür.
import pandas as pd
import numpy as np
#Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack']),
   'Age':pd.Series([25,26,25,23,30,29,23]),
   'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#Create a DataFrame
df = pd.DataFrame(d)
print ("The data types of each column are:")
print df.dtypesOnun output aşağıdaki gibidir -
The data types of each column are:
Age     int64
Name    object
Rating  float64
dtype: objectboş
Object'in boş olup olmadığını söyleyen Boolean değerini döndürür; True, nesnenin boş olduğunu gösterir.
import pandas as pd
import numpy as np
 
#Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack']),
   'Age':pd.Series([25,26,25,23,30,29,23]),
   'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
 
#Create a DataFrame
df = pd.DataFrame(d)
print ("Is the object empty?")
print df.emptyOnun output aşağıdaki gibidir -
Is the object empty?
Falsendim
Nesnenin boyutlarının sayısını verir. DataFrame tanımı gereği 2B bir nesnedir.
import pandas as pd
import numpy as np
#Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack']),
   'Age':pd.Series([25,26,25,23,30,29,23]),
   'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#Create a DataFrame
df = pd.DataFrame(d)
print ("Our object is:")
print df
print ("The dimension of the object is:")
print df.ndimOnun output aşağıdaki gibidir -
Our object is:
      Age    Name     Rating
0     25     Tom      4.23
1     26     James    3.24
2     25     Ricky    3.98
3     23     Vin      2.56
4     30     Steve    3.20
5     29     Smith    4.60
6     23     Jack     3.80
The dimension of the object is:
2şekil
DataFrame'in boyutluluğunu temsil eden bir demet döndürür. Tuple (a, b), burada a satır sayısını temsil eder veb sütun sayısını temsil eder.
import pandas as pd
import numpy as np
 
#Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack']),
   'Age':pd.Series([25,26,25,23,30,29,23]),
   'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
 
#Create a DataFrame
df = pd.DataFrame(d)
print ("Our object is:")
print df
print ("The shape of the object is:")
print df.shapeOnun output aşağıdaki gibidir -
Our object is:
   Age   Name    Rating
0  25    Tom     4.23
1  26    James   3.24
2  25    Ricky   3.98
3  23    Vin     2.56
4  30    Steve   3.20
5  29    Smith   4.60
6  23    Jack    3.80
The shape of the object is:
(7, 3)boyut
DataFrame'deki öğelerin sayısını döndürür.
import pandas as pd
import numpy as np
 
#Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack']),
   'Age':pd.Series([25,26,25,23,30,29,23]),
   'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
 
#Create a DataFrame
df = pd.DataFrame(d)
print ("Our object is:")
print df
print ("The total number of elements in our object is:")
print df.sizeOnun output aşağıdaki gibidir -
Our object is:
    Age   Name    Rating
0   25    Tom     4.23
1   26    James   3.24
2   25    Ricky   3.98
3   23    Vin     2.56
4   30    Steve   3.20
5   29    Smith   4.60
6   23    Jack    3.80
The total number of elements in our object is:
21değerler
DataFrame'deki gerçek verileri bir NDarray.
import pandas as pd
import numpy as np
 
#Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack']),
   'Age':pd.Series([25,26,25,23,30,29,23]),
   'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
 
#Create a DataFrame
df = pd.DataFrame(d)
print ("Our object is:")
print df
print ("The actual data in our data frame is:")
print df.valuesOnun output aşağıdaki gibidir -
Our object is:
    Age   Name    Rating
0   25    Tom     4.23
1   26    James   3.24
2   25    Ricky   3.98
3   23    Vin     2.56
4   30    Steve   3.20
5   29    Smith   4.60
6   23    Jack    3.80
The actual data in our data frame is:
[[25 'Tom' 4.23]
[26 'James' 3.24]
[25 'Ricky' 3.98]
[23 'Vin' 2.56]
[30 'Steve' 3.2]
[29 'Smith' 4.6]
[23 'Jack' 3.8]]Kuyruk
Bir DataFrame nesnesinin küçük bir örneğini görüntülemek için, head() ve tail () yöntemleri. head() ilkini döndürür nsatırlar (dizin değerlerine dikkat edin). Görüntülenecek varsayılan öğe sayısı beştir, ancak özel bir sayı iletebilirsiniz.
import pandas as pd
import numpy as np
 
#Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack']),
   'Age':pd.Series([25,26,25,23,30,29,23]),
   'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#Create a DataFrame
df = pd.DataFrame(d)
print ("Our data frame is:")
print df
print ("The first two rows of the data frame is:")
print df.head(2)Onun output aşağıdaki gibidir -
Our data frame is:
    Age   Name    Rating
0   25    Tom     4.23
1   26    James   3.24
2   25    Ricky   3.98
3   23    Vin     2.56
4   30    Steve   3.20
5   29    Smith   4.60
6   23    Jack    3.80
The first two rows of the data frame is:
   Age   Name   Rating
0  25    Tom    4.23
1  26    James  3.24tail() sonuncuyu döndürür nsatırlar (dizin değerlerine dikkat edin). Görüntülenecek varsayılan öğe sayısı beştir, ancak özel bir sayı iletebilirsiniz.
import pandas as pd
import numpy as np
#Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack']),
   'Age':pd.Series([25,26,25,23,30,29,23]), 
   'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
 
#Create a DataFrame
df = pd.DataFrame(d)
print ("Our data frame is:")
print df
print ("The last two rows of the data frame is:")
print df.tail(2)Onun output aşağıdaki gibidir -
Our data frame is:
    Age   Name    Rating
0   25    Tom     4.23
1   26    James   3.24
2   25    Ricky   3.98
3   23    Vin     2.56
4   30    Steve   3.20
5   29    Smith   4.60
6   23    Jack    3.80
The last two rows of the data frame is:
    Age   Name    Rating
5   29    Smith    4.6
6   23    Jack     3.8