Pandas Basic tutorial 2 - Python Programming |
Open
Anaconda Navigator
Launch
Sypder
PROGRAM 1
import pandas as pd
d = pd.DataFrame()
print (d)
OUTPUT
Empty DataFrame
Columns: []
Index: []
PROGRAM
2
import pandas as pd
listdata = [10,9,8,7,6,5]
d = pd.DataFrame(listdata)
print (d)
OUTPUT
0
0 10
1 9
2 8
3 7
4 6
5 5
PROGRAM
3
import pandas as pd
a =
[['Sumi',21.5],['Sobi',17.8],['Caddie',14.9],['Drake',12.6],['Rita',18.4]]
d = pd.DataFrame(a,columns=['Name of the student','Age of the
student'],dtype=float)
print (d)
OUTPUT
Name of the student Age of the student
0 Sumi
21.5
1 Sobi
17.8
2 Caddie
14.9
3 Drake
12.6
4 Rita
18.4
PROGRAM 4
import pandas as pd
data = {'Name of the student':['Sumi', 'Sobi', 'Caddie',
'Drake','Rita'],'Age of the student':[21.5,17.8,14.9,12.6,18.4]}
d = pd.DataFrame(data, index=['50','51','52','53','54'])
print (d)
OUTPUT
Name of the student Age of the student
50 Sumi
21.5
51 Sobi
17.8
52 Caddie
14.9
53 Drake
12.6
54 Rita
18.4
PROGRAM
5
import pandas as pd
a = {'Names' : pd.Series(['Somi','Sobi','Tina'], index=['a', 'b', 'c']),
'Ranks' : pd.Series([1, 2, 3, 4], index=['a', 'b',
'c', 'd'])}
d = pd.DataFrame(a)
print (d)
print('Names Selection')
print (d ['Names'])
print('Ranks Selection')
print(d['Ranks'])
print ("Adding a new data ")
d['Names']=pd.Series(['Pooja','Adhya'],index=['a','d'])
print (d)
print ("Deleting the first column ")
del d['Names']
print (d)
OUTPUT
Names Ranks
a Somi 1
b Sobi 2
c Tina 3
d NaN 4
Names Selection
a Somi
b Sobi
c Tina
d NaN
Name: Names, dtype: object
Ranks Selection
a 1
b 2
c 3
d 4
Name: Ranks, dtype: int64
Adding a new data
Names Ranks
a Pooja 1
b NaN 2
c NaN 3
d Adhya 4
Deleting the first column
Ranks
a 1
b 2
c 3
d 4
PROGRAM
6
import pandas as pd
a = {'Column1' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
'Column2' : pd.Series([3,4,5,6], index=['a', 'b',
'c', 'd'])}
d = pd.DataFrame(a)
print (d.loc['a'])
print (d.loc['d'])
print (d.iloc[3])
print (d[:3])
print (d[2:])
print (d[1:4])
OUTPUT
Column1 1.0
Column2 3.0
Name: a, dtype: float64
Column1 NaN
Column2 6.0
Name: d, dtype: float64
Column1 NaN
Column2 6.0
Name: d, dtype: float64
Column1 Column2
a 1.0 3
b 2.0 4
c 3.0 5
Column1 Column2
c 3.0 5
d NaN 6
Column1 Column2
b 2.0 4
c 3.0 5
d NaN 6
PROGRAM
7
import pandas as pd
d1 = pd.DataFrame([[4,5], [-1,7],[2,-8]], columns =
['Column1','Column2'])
d2 = pd.DataFrame([[1,0], [3,9],[-4,7]], columns =
['Column1','Column2'])
d1 = d1.append(d2)
print('After appending')
print (d1)
print('dropping index 1 values')
d1 = d1.drop(1)
print (d1)
OUTPUT
After appending
Column1 Column2
0 4 5
1 -1 7
0 1 0
1 3 9
dropping index 1 values
Column1 Column2
0 4 5
0 1 0
runfile('C:/Users/dell/temp.py', wdir='C:/Users/dell')
After appending
Column1 Column2
0 4 5
1 -1 7
2 2 -8
0 1 0
1 3 9
2 -4 7
dropping index 1 values
Column1 Column2
0 4 5
2 2 -8
0 1 0
2 -4 7