A data frame is a two-dimensional data structure in which data is arranged in tabular rows and columns.
We can perform basic operations on rows or columns, such as select, delete, add, and rename.
Nba.csv files will be used today.
Processing column
To process the columns, we perform basic operations on the columns, such as select, delete, add, and rename.
The column options:
To select a column in the PandasDataFrame, we can access them by calling their column names.
# Import pandas package
import pandas as pd
# Define a dictionary containing employee data
data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],
'Age':[27, 24, 22, 32],
'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],
'Qualification':['Msc', 'MA', 'MCA', 'Phd']}
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
# select two columns
print(df[['Name', 'Qualification']])
Copy the code
Output:
Column addition:
Add a column in PandasDataFrame to declare a new list as a column and add it to an existing Dataframe.
# Import pandas package import pandas as pd # Define a dictionary containing Students data data = {'Name': [' Jai ', 'Princi', 'Gaurav', 'Anuj],' Height ': [5.1, 6.2, 5.1, 5.2],' Qualification ': ['Msc', 'MA', 'Msc', 'Msc']} # Convert the dictionary into DataFrame df = pd.DataFrame(data) # Declare a list that is to be converted into a column address = ['Delhi', 'Bangalore', 'Chennai', 'Patna'] # Using 'Address' as the column name # and equating it to the list df['Address'] = address # Observe the result print(df)Copy the code
Output:
Delete:
To drop the column in Pandas DataFrame, use the drop() method. Delete columns by deleting columns with column names.
# importing pandas module
import pandas as pd
# making data frame from csv file
data = pd.read_csv("nba.csv", index_col ="Name" )
# dropping passed columns
data.drop(["Team", "Weight"], axis = 1, inplace = True)
# display
print(data)
Copy the code
Output:
The new output has no columns to pass. These values are deleted.
Because AXIS is set to 1, and because Inplace is True, the change is made in the original data frame.
Delete column before data frame –
Delete column after data frame –
Processing line:
Performing basic operations on rows includes selecting, deleting, adding, and renaming.
Row selection: Panda provides a unique way to retrieve rows from a data framework. The datafame.loc [] method is used to retrieve rows from the Pandas DataFrame. The line can also be used by passing the integer position to the Ilock [] function.
# importing pandas package
import pandas as pd
# making data frame from csv file
data = pd.read_csv("nba.csv", index_col ="Name")
# retrieving row by loc method
first = data.loc["Avery Bradley"]
second = data.loc["R.J. Hunter"]
print(first, "\n\n\n", second)
Copy the code
Output: As shown in the output image, two sequences are returned because both times there is only one parameter.
Add:
Add a Row to the PandasDataFrame to connect the old dataframe to the new data.
# importing pandas module
import pandas as pd
# making data frame
df = pd.read_csv("nba.csv", index_col ="Name")
df.head(10)
new_row = pd.DataFrame({'Name':'Geeks', 'Team':'Boston', 'Number':3,
'Position':'PG', 'Age':33, 'Height':'6-2',
'Weight':189, 'College':'MIT', 'Salary':99999},
index =[0])
# simply concatenate both dataframes
df = pd.concat([new_row, df]).reset_index(drop = True)
df.head(5)
Copy the code
Output:
Add row before data frame –
Add row after data frame –
Delete:
Drop a row in PandasDataFrame with the Drop() method, by dropping the row by index label.
# importing pandas module
import pandas as pd
# making data frame from csv file
data = pd.read_csv("nba.csv", index_col ="Name" )
# dropping passed values
data.drop(["Avery Bradley", "John Holland", "R.J. Hunter",
"R.J. Hunter"], inplace = True)
# display
data
Copy the code
Output: As shown in the output image, the new output has no passed value. These values are removed and changed in the original data frame because Inplace is True.
Data frames before deleting values –
Delete value after data frame –