In [1]:
import pandas as pd
In [2]:
"""1D struktura podataka"""

s1 = pd.Series([1,2,3,5]) #pravljenje serije pomocu liste i automatsko dodeljivanje oznaka redovima
In [3]:
print(s1)
0    1
1    2
2    3
3    5
dtype: int64
In [4]:
#pristup elementima  serija[oznaka]
print('s1[2]', s1[2])
print('s1[1]', s1.get(1))
s1[2] 3
s1[1] 2
In [5]:
import numpy as np  # modul zbog nan vrednosti
print('s1[8]', s1.get(8, np.nan))
s1[8] nan
In [6]:
# pravljenje serije pomocu liste sa zadatim oznakama za redove
s2 = pd.Series([1,2,3,5], index=['a', 'b', 'c', 'd'])
In [7]:
print("s2['d']" ,s2['d'])
s2['d'] 5
In [8]:
s3 = pd.Series({'e':2, 'a':1,'b':2,'c':3,'d':5, }) # pravljenje serije pomocu recnika
print(s3)
print("s3['a']", s3['a'])
e    2
a    1
b    2
c    3
d    5
dtype: int64
s3['a'] 1
In [9]:
#izdvajanje oznaka
print(s3.index.tolist())
#ili
print(s3.keys())
['e', 'a', 'b', 'c', 'd']
Index(['e', 'a', 'b', 'c', 'd'], dtype='object')
In [10]:
#izdvajanje vrednosti
print(s3.values)
[2 1 2 3 5]
In [11]:
#tip elemenata
print(s3.dtype)
int64
In [12]:
#broj pojavljivanja svake vrednosti
print(s3.value_counts())
2    2
1    1
3    1
5    1
dtype: int64
In [13]:
s2
Out[13]:
a    1
b    2
c    3
d    5
dtype: int64
In [14]:
s3
Out[14]:
e    2
a    1
b    2
c    3
d    5
dtype: int64
In [15]:
s2+s3
Out[15]:
a     2.0
b     4.0
c     6.0
d    10.0
e     NaN
dtype: float64
In [16]:
s2+s2
Out[16]:
a     2
b     4
c     6
d    10
dtype: int64
In [17]:
"""2D struktura podataka DataFrame"""

d1= {
    'prva': pd.Series([1,2,3], index=['a', 'b', 'c']),
    'druga': pd.Series(['x', 'y', 'z'], index=['a', 'b', 'c'])
}
df1 = pd.DataFrame(d1)

print("df1")
print(df1)
df1
   prva druga
a     1     x
b     2     y
c     3     z
In [18]:
d2= {
    'prva': pd.Series([3,2,1], index=['a', 'b', 'c']),
    'druga': pd.Series(['x', 'z', 'y'], index=['a', 'b', 'd']),
    'treca': pd.Series(['m', 'n', 'p'], index=['a', 'b', 'e'])
   
}
In [19]:
df2 = pd.DataFrame(d2)
In [20]:
df2
Out[20]:
prva druga treca
a 3.0 x m
b 2.0 z n
c 1.0 NaN NaN
d NaN y NaN
e NaN NaN p
In [21]:
#df3 = pd.DataFrame( [(1,2,'X'), (2,3,'C')], columns=['A', 'B','C'])
df3 = pd.DataFrame( [(1,2,'X'), (2,3,'C')])  
In [22]:
df3
Out[22]:
0 1 2
0 1 2 X
1 2 3 C
In [23]:
#sortiranje po indeksima (oznakama)
#df2.sort_index(ascending=False, inplace=True)  # sortiranje redova
df2.sort_index(ascending=True, inplace=True)  # sortiranje redova
In [24]:
df2
Out[24]:
prva druga treca
a 3.0 x m
b 2.0 z n
c 1.0 NaN NaN
d NaN y NaN
e NaN NaN p
In [25]:
df2.sort_index(ascending=True, axis=1, inplace=True) #sortiranje kolona
In [26]:
df2
Out[26]:
druga prva treca
a x 3.0 m
b z 2.0 n
c NaN 1.0 NaN
d y NaN NaN
e NaN NaN p
In [27]:
#sortiranje po vrednosti kolone
df2.sort_values(by='prva', inplace=True)   #by=[lista kolona]
In [28]:
df2
Out[28]:
druga prva treca
c NaN 1.0 NaN
b z 2.0 n
a x 3.0 m
d y NaN NaN
e NaN NaN p
In [29]:
df4 = pd.DataFrame({
                    'number' : [1,2,2,3],
                    'object' : ['c', 'd', 'e', 'c']
                    })
In [30]:
print(df4.dtypes)
number     int64
object    object
dtype: object
In [31]:
print("Opis svih kolona")
#print(df4.describe(include='all'))
print(df4.describe())
Opis svih kolona
         number
count  4.000000
mean   2.000000
std    0.816497
min    1.000000
25%    1.750000
50%    2.000000
75%    2.250000
max    3.000000
In [32]:
print("Opis numerickih kolona")
print(df4.describe(include=['number']))
Opis numerickih kolona
         number
count  4.000000
mean   2.000000
std    0.816497
min    1.000000
25%    1.750000
50%    2.000000
75%    2.250000
max    3.000000
In [33]:
print("Opis kolona object")
print(df4.describe(include=['object']))
Opis kolona object
       object
count       4
unique      3
top         c
freq        2
In [34]:
df2
Out[34]:
druga prva treca
c NaN 1.0 NaN
b z 2.0 n
a x 3.0 m
d y NaN NaN
e NaN NaN p
In [35]:
#neke deskriptivne statistike
print("Srednje vrednosti")
print(df2.mean())
Srednje vrednosti
prva    2.0
dtype: float64
In [36]:
print("Srednje vrednosti uzimajuci u obzir NaN")
print(df2.mean(skipna=False))
Srednje vrednosti uzimajuci u obzir NaN
prva   NaN
dtype: float64
In [37]:
df5=pd.DataFrame({'a':[1,2,3], 'b':[2,8,10]})
print(df5)
print("Srednja vrednost za svaki red")
print(df5.mean(axis=1))
   a   b
0  1   2
1  2   8
2  3  10
Srednja vrednost za svaki red
0    1.5
1    5.0
2    6.5
dtype: float64
In [38]:
print("Zbir po kolonama")
print(df5.sum())
Zbir po kolonama
a     6
b    20
dtype: int64
In [39]:
print("Zbir po redovima")
print(df5.sum(axis=1))
Zbir po redovima
0     3
1    10
2    13
dtype: int64
In [40]:
print("Percentili")
print(df5.quantile())
print(df5.quantile(0.25))
print(df5.quantile([0.25, 0.5,0.75]))
Percentili
a    2.0
b    8.0
Name: 0.5, dtype: float64
a    1.5
b    5.0
Name: 0.25, dtype: float64
        a    b
0.25  1.5  5.0
0.50  2.0  8.0
0.75  2.5  9.0
In [41]:
print("Najzastupljenije vrednosti")
print(df2.mode())
Najzastupljenije vrednosti
  druga  prva treca
0     x   1.0     m
1     y   2.0     n
2     z   3.0     p
In [42]:
d2= {
    'prva': pd.Series([3,2,1], index=['a', 'b', 'c']),
    'druga': pd.Series(['x', 'x', 'y'], index=['a', 'b', 'd']),
    'treca': pd.Series(['m', 'n', 'p'], index=['a', 'b', 'e'])
   
}
df2=pd.DataFrame(d2)
In [43]:
print("Najzastupljenije vrednosti")
print(df2.mode())
Najzastupljenije vrednosti
   prva druga treca
0   1.0     x     m
1   2.0   NaN     n
2   3.0   NaN     p
In [44]:
print("Razlicite vrednosti")
print(df2.nunique())
Razlicite vrednosti
prva     3
druga    2
treca    3
dtype: int64
In [ ]: