Python DATAFRAME Basic Functions and Operations
CREATING DATAFRAME
import pandas as pd
a= {'PNAME':['TV','AC','TV','WM','AC','TV'],
'COMP':['LG','WIRLPOOL','SONY','WIRLPOOL','LG','LG'],
'PRICE':[10000,25000,15000,12000,28000,15000],
'QTY':[2,5,15,8,12,5]}
df = pd.DataFrame(a,index=['P1','P2','P3','P4','P5','P6'])
*********************************************
FETCH SINGLE COLUMN
print(df['PNAME'])
OR
print(df.PNAME)
OUTPUT
*********************************************
FETCH MULTIPLE COLUMNS
print(df[['PNAME','PRICE']])
OUTPUT
*********************************************
FETCH SINGLE ROW
print(df.loc['P2'])
OR
print(df.iloc[1])
OUTPUT
*********************************************
FETCH MULTIPLE ROWS
print(df.loc[['P2','P5']])
OUTPUT
*********************************************
FETCH SOME ROWS, SOME COLUMNS
print(df.loc[['P2','P5'],['PNAME','PRICE']])
OUTPUT
*********************************************
FETCH SINGLE VALUE
print(df['COMP']['P3'])
OUTPUT
*********************************************
FETCH ALL VALUES HAVING QTY>5
print(df[ (df['QTY']>5) ])
OUTPUT
*********************************************
FETCH TOP 3 ROWS
print(df.head(3))
OUTPUT
*********************************************
FETCH LAST 3 ROWS
print(df.tail(3))
OUTPUT
*********************************************
ADDING NEW COLUMNdf['DISCOUNT'] = [1,4,2,6,8,3]
OR
df.loc[:,'DISCOUNT'] = [1,4,2,6,8,3]
OR
df.at[:,'DISCOUNT'] = [1,4,2,6,8,3]
OUTPUT
*********************************************
ADDING NEW ROW
df.loc['P10',:] = ['OS', 'ANDROID', 5000, 6]
OR
df.at['P10',:] = ['OS', 'ANDROID', 5000, 6]
OUTPUT
*********************************************
DELETING A COLUMN
del df['COMP']
OR
df.drop(['COMP'],axis=1,inplace=True)
OR
df.drop(df.columns[1],axis=1,inplace=True)
OUTPUT
*********************************************
DELETING MULTIPLE COLUMNS
df.drop(['PNAME','PRICE'],axis=1,inplace=True)
OUTPUT
*********************************************
DELETING A ROW
df.drop(['P1'],axis=0,inplace=True)
OR
df.drop(df.index[0],axis=0,inplace=True)
OUTPUT
*********************************************
DELETING MULTIPLE ROWS
df.drop(['P1','P2'],axis=0,inplace=True)
OUTPUT
*********************************************
UPDATE COLUMN VALUES
df['PRICE'] = [1,2,3,4,5,6]
*********************************************
UPDATE COLUMN WITH A SINGLE VALUE
df['PRICE'] = 5
*********************************************
UPDATE A ROW WITH A SINGLE VALUE
df.loc['P1',:] = 5
*********************************************
UPDATE A ROW WITH DIFFERENT VALUES
df.loc['P1',:] = [1,2,3,4]
*********************************************
UPDATE A SPECIFIC RECORD
df['PRICE']['P1'] = 5
OR df.loc['P1','PRICE'] = 5 OR df.at['P1','PRICE'] = 5
*********************************************
MISC FUNCTIONS
*********************************************
INCREASE VALUE OF QTY COLUMN BY 10
df['QTY'] += 10
*********************************************
SORT DATAFRAME BY QTY
df.sort_values(axis=0,ascending=False,inplace=True,by=['QTY'])
*********************************************
SUM OF QTY COLUMN
print('SUM OF COLUMN IS = ', df['QTY'].sum())
*********************************************
MINIMUM AND MAXIMUM VALUE FROM QTY COLUMN
print('MINIMUM QTY IS = ', df['QTY'].min())
print('MAXIMUM QTY IS = ', df['QTY'].max())
*********************************************
MINIMUM AND MAXIMUM VALUE FROM EACH COLUMN
print('MINIMUM IS = \n', df.min())
print('MAXIMUM IS = \n', df.max())
AS
as
No comments:
Post a Comment