IP 12th Chapter 3
IP 12th Chapter 3
Creation of Dataframe
1. Empty Dataframe
import pandas as pd
df = pd.DataFrame()
print(df)
DataFrame Indexing
● DataFrame indexing refers to the process of selecting and retrieving specific data
elements from a DataFrame.
● It allows you to access and manipulate the data within a DataFrame using various
methods and techniques.
● Indexing in a DataFrame can be performed in multiple ways:
1. Label-based Indexing
● This indexing method is primarily used for label-based indexing, allowing
you to access data based on row and column labels.
● You can use it to retrieve specific rows or columns by providing the labels
as arguments.
● Syntax : df.loc[row_label, column_label]
2. Boolean Indexing
● Boolean indexing involves using a Boolean condition to filter the
DataFrame.
● It allows you to select rows or columns based on certain conditions using
logical operators such as ==, >, <, >=, <=, !=, and combining them with
logical operators like & (and) and | (or).
● Syntax : df[df['column_name'] > 10]
Traversing of Dataframe
● The iterrows() method allows you to iterate over the rows of a DataFrame, returning
each row as a tuple containing the index and row data.
● Syntax
for index, row in df.iterrows():
# Access row data
print(index, row['Column1'], row['Column2'])
● Example :
import pandas as pd
● Column Deletion : To delete a column from a DataFrame, you can use the
drop() method.
# Delete a single column
df = df.drop('ColumnName', axis=1)
2. Operation on Row
● Row Selection : To select a specific row or multiple rows from a
DataFrame, you can use the loc[] or iloc[] accessor.
# Select a single row by index label
single_row = df.loc[3]
● Row Addition : To add a new row to a DataFrame, you can use the loc[]
accessor along with the assignment operator.
● Row Deletion : To delete a row from a DataFrame, you can use the drop()
method.
# Delete a single row by index label
df = df.drop(3)
Methods of Dataframe
1. head( )
● The head() method is used to retrieve the first n rows of a DataFrame.
● By default, if no argument is provided, it returns the first 5 rows.
● Example
import pandas as pd
2. tail( )
● The tail() method is the counterpart of head(). It is used to retrieve the last n
rows of a DataFrame.
● By default, if no argument is provided, it returns the last 5 rows.
● Example
import pandas as pd
print(df)