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

DataFrame Operations and Syntax Guide

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)
7 views3 pages

DataFrame Operations and Syntax Guide

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

DataFrame Syntax

Empty Dataframe
Df=[Link]()
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
[Link]({‘old column name’:‘new column name’, old column
name1’:‘new column name1’},axis=’columns’)
Deleting columns
[Link](‘column’,axis=1) for deleting column

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

loc[] and iloc []


Accesses a set of rows and columns
[Link][Start row:End row,Start col:End col]
Accesses a single row
[Link][row label, : ]
Accesses a set of rows
[Link][Start row label: End row label, : ]
Accesses a set of columns
[Link][ : , Start column label : End column label]
[Link][Start row index : End row index, Start col index: End col index]
Accesses a single row
[Link][row index, : ]
Accesses a set of rows
[Link][Start row index : End row index, : ]
Accesses a set of columns
[Link][ : ,Start column index : End column index]
DF[:]= ‘value’ entire data frame gets this ‘value’
Default:
Print([Link][0]) Accessing row
Print([Link][ : ,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
[Link](n) Print first n number of columns
[Link](n) Prints last n number of columns
Count and transpose
[Link]() 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
[Link][‘row label’] > ‘value’
[Link][: ,‘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