Pandas Session 3 Code

Pandas Session 3 Code For Video Click Fahad Hussain CS


Hi, Welcome to Fahad Hussain CS blog, do not forget to subscribe and follow the blog and YouTube channel  Fahad Hussain CS

Code:

import pandas as pd

import numpy as np


obj = pd.Series(range(3), index=['a', 'b', 'c'])

index = obj.index

print(index)

print(index[-1:])


labels = pd.Index(np.arange(3))

print(labels)


obj2 = pd.Series([1.5, -2.5, 0], index=labels)

print(obj2)

print(obj2.index is labels)


data = {'State':['Khi','Lhr','Mul','Isl','Haz','Raw'],

'Year':[2000,2001,2002,2003,2004,2005],

'Pop':[1.5,1.6,2.5,4.2,3.5,1.9]}


frame1 = pd.DataFrame(data)


frame1.columns

print('State' in frame1.columns)

print(2003 in frame1.index)


Sdup_labels = pd.Index(['foo', 'foo', 'bar', 'bar'])

dup_labels


print(frame1[0::])

print(frame1[::])

print(frame1[0::-1])

print(frame1[0::1])


print(frame1.loc[:,['State']])


###Essential Functionality

obj = pd.Series([4.5, 7.2, -5.3, 3.6], index=['d', 'b', 'a', 'c'])

obj


obj2 = obj.reindex(['a', 'b', 'c', 'd', 'e'])

obj2


obj3 = pd.Series(['blue', 'purple', 'yellow'], index=[0, 2, 4])

print(obj3)

print(obj3.reindex(range(6), method='ffill'))


frame = pd.DataFrame(np.arange(9).reshape((3, 3)),

                     index=['a', 'c', 'd'],

                     columns=['Ohio', 'Texas', 'California'])

print(frame)

frame2 = frame.reindex(['a', 'b', 'c', 'd'])

print(frame2)


states = ['Texas', 'Utah', 'California']

frame.reindex(columns=states)

print(frame)


obj = pd.Series(np.arange(5.), index=['a', 'b', 'c', 'd', 'e'])

print(obj)

new_obj = obj.drop('c')

print(new_obj)

print(obj.drop(['d', 'c']))


data = pd.DataFrame(np.arange(16).reshape((4, 4)),

                    index=['Ohio', 'Colorado', 'Utah', 'New York'],

                    columns=['one', 'two', 'three', 'four'])

data


print(data.drop(['Colorado', 'Ohio']))



print(data)

print(data.drop('two', axis=1))

print(data.drop(['two', 'four'], axis='columns'))


####Indexing, Selection, and Filtering


obj = pd.Series(np.arange(4.), index=['a', 'b', 'c', 'd'])

print(obj)

print(obj['b'])

print(obj[1])

print(obj[2:4])

print(obj[['b', 'a', 'd']])

print(obj[[1, 3]])

print(obj[obj < 2])


print(obj['b':'c'])


data = pd.DataFrame(np.arange(16).reshape((4, 4)),

                    index=['Ohio', 'Colorado', 'Utah', 'New York'],

                    columns=['one', 'two', 'three', 'four'])

print(data)

print(data['two'])

print(data[['three', 'one']])


print(data[::1])

print(data[data['three'] > 5])


data < 5

data[data < 5] = 0

data


###Selection with loc and iloc


print(data.loc['Colorado', ['two', 'three']])


print(data)

print(data.iloc[2, [3, 0, 1]])

print(data.iloc[2])

print(data.iloc[[1, 2], [3, 0, 1]])


print(data.loc[:'Utah', 'two'])

print(data.iloc[:, :3][data.three > 5])


No comments:

Post a Comment

Fell free to write your query in comment. Your Comments will be fully encouraged.