0% found this document useful (0 votes)
50 views

Pandas Attribute

This document demonstrates various attributes and methods of the Pandas DataFrame object in Python. It shows how to create DataFrames from dictionaries of data, access columns and rows, add and remove columns, modify cell values, rename indexes and columns, and use indexing to select subsets of the data. Various print statements are used to display the results of attribute calls and indexing operations on the DataFrames.

Uploaded by

Manish Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Pandas Attribute

This document demonstrates various attributes and methods of the Pandas DataFrame object in Python. It shows how to create DataFrames from dictionaries of data, access columns and rows, add and remove columns, modify cell values, rename indexes and columns, and use indexing to select subsets of the data. Various print statements are used to display the results of attribute calls and indexing operations on the DataFrames.

Uploaded by

Manish Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Python Pandas Attributes

import pandas as pd

dict1={'section':['a','b','c','d'],'contri':[6700,5600,5000,5200]}

df1=pd.DataFrame(dict1)

print(df1)

df2=pd.DataFrame(dict1,index=['I','II','III','IV'])

print(df2)

print(df2.values) # values from data frame object

print(df2.index) # index no from data frame object

print(df2.columns) # to display columns of dataframe obj

print(df2.contri) # to display particular column values.

print(df2.axes) # to display axes values

print(len(df2)) # to display total no of records.

print(df2.count()) # to display total values in columns

print(df2.count(1)) # to display total values in rows

print(df2.T) # to make transpose data frame

print(df2.section) # to display values of column

print(df2['section']) # to display values of column

print(df2.section,df2.contri) # to display values of column

print(df2)

print(df2.loc['I']) # to show values of row via rowlable

print(df2.loc['II':'IV']) # to show values of row via rowlable

print(df2.iloc[2:]) # to display values of row via index no.(start from 0)

print(df2[0:2]) # to display first two records

df2['classname']=12 # to add new column with values


df2['classname']=[11,11,12,10] # to change new col values

df2.at['IV']=['g',5000] # to change row values

df2.at['V']=['h',5000] # if no row label then it will add as new row

del df2['contri'] # to remove any col with its values

df2.section['IV']='z' # to modify particular cell value

df1.drop(0) # remove the row as per index no

df2.drop('I') # remove the row as per own index no

df2.rename(index={'I':'A','II':'B','III':'C','IV':'D'},columns={'section':'st_section'},inplace=True)

# to change the index no and column heading

DataFrame Indexing

import pandas as pd

Days=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']

Classes=[6,1,3,4,8,0,2]

dc={'Days':Days,'No. of Classes':Classes}

classDf=pd.DataFrame(dc,index=[True,False,True,False,True,False,False])

print(classDf)

print(classDf.loc[True])

print(classDf.loc[False])

You might also like