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:

! [](https://p1.pstatp.com/origin/pgc-image/f3ddc230c3af43718a93dbdea527a953)

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:

! [](https://p1.pstatp.com/origin/pgc-image/24f363e6074c4693af52113c2d7f8dd4)

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 –

! [](https://p3-tt-ipv6.byteimg.com/origin/pgc-image/8332905cbdef4dddbc2672c07df71b82)

Delete column after data frame –

! [](https://p6-tt-ipv6.byteimg.com/origin/pgc-image/5b2b1ba79428433fa3db0980cda341d4)

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.

! [](https://p1.pstatp.com/origin/pgc-image/f4e984e60e6e4d909614f6a5397c617c)

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 –

! [](https://p1-tt-ipv6.byteimg.com/origin/pgc-image/a3d7c3a4f62b4ecd83a267bdedf332d2)

Add row after data frame –

! [](https://p1-tt-ipv6.byteimg.com/origin/pgc-image/75bb8bcf7bce404f828132e1f0c0335c)

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 –

! [](https://p3-tt-ipv6.byteimg.com/origin/pgc-image/4c260eb4e2b54a1b82819a2dd0763a1d)

Delete value after data frame –

! [](https://p6-tt-ipv6.byteimg.com/origin/pgc-image/66e106bd61e842f3bca099377ba6ebb1)