0% found this document useful (0 votes)
13 views3 pages

Dataframe_Syntax

Uploaded by

juliusjuan142
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Dataframe_Syntax

Uploaded by

juliusjuan142
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

DataFrame Syntax

Empty Dataframe
Df=pd.DataFrame()
o/p
Empty DataFrame
Columns:[]
Index:[]

COLUMNS
Accessing columns
DF[‘column name’]
DF[[‘col1’,’col2’,’col3’]]
Modify column values
DF[‘existing column’]=[values]
Adding new column
DF[‘new column’]=[values]
Rename Column Values
DF.rename({‘old column name’:‘new column name’, old column
name1’:‘new column name1’},axis=’columns’)
Deleting columns
DF.drop(‘column’,axis=1) for deleting column

ROWS
Adding new row
DF.loc[‘new row’]=[values]
Rename row Values
DataFrame Syntax
DF.rename({‘old row name’:‘new row name’, old row name1’:‘new row
name1’},axis=’index’)
Deleting row
DF.drop(‘row’,axis=0) for deleting row

loc[] and iloc []


Accesses a set of rows and columns
DF.loc[Start row:End row,Start col:End col]
Accesses a single row
DF.loc[row label, : ]
Accesses a set of rows
DF.loc[Start row label: End row label, : ]
Accesses a set of columns
DF.loc[ : , Start column label : End column label]
DF.iloc[Start row index : End row index, Start col index: End col index]
Accesses a single row
DF.iloc[row index, : ]
Accesses a set of rows
DF.iloc[Start row index : End row index, : ]
Accesses a set of columns
DF.iloc[ : ,Start column index : End column index]
DF[:]= ‘value’ entire data frame gets this ‘value’
Default:
Print(df.loc[0]) Accessing row
Print(df.iloc[ : ,0]) Accessing column
DataFrame Syntax

Attributes
<DF>.index
<DF>.columns
<DF>.axis
<DF>.values
<DF>.dtypes
<DF>.size Number of elements including NaN
<DF>.ndim
<DF>.empty True/False
head and tail
DF.head(n) Print first n number of columns
DF.tail(n) Prints last n number of columns
Count and transpose
DF.count() counts and shows no of values in each column exclude NaN
DF.T changes the row elements into column elements and the vice versa.
Boolean Indexing
DF.loc[‘row label’] > ‘value’
DF.loc[: ,‘column label’]> ‘value’
Importing a CSV file to a DataFrame
DF = pd.read_csv("file path",sep =",", header=0)
To save a DataFrame to a text or csv file
DF.to_csv(‘file path’, sep=',')
If we do not want the column names and row names to be saved to the file
DF.to_csv( ‘file path’,sep = '@', header = False, index= False)

You might also like