Dealing with Rows and Columns in Pandas DataFrame - GeeksforGeeks

13 Oct, 2021
A Data frame is a two-dimensional data structure, i.e., data is aligned in a tabular fashion in rows and columns. We can perform basic operations on rows/columns like selecting, deleting, adding, and renaming. In this article, we are using nba.csv file.
In order to deal with columns, we perform basic operations on columns like selecting, deleting, adding and renaming.
Column Selection:In Order to select a column in Pandas DataFrame, we can either access the columns by calling them by their columns name.
# Import pandas packageimport pandas as pd  # Define a dictionary containing employee datadata = {'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 columnsprint(df[['Name', 'Qualification']])
Output:For more examples refer to How to select multiple columns in a pandas dataframe Column Addition:In Order to add a column in Pandas DataFrame, we can declare a new list as a column and add to a existing Dataframe.
# Import pandas package import pandas as pd  # Define a dictionary containing Students datadata = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'],        'Height': [5.1, 6.2, 5.1, 5.2],        'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}  # Convert the dictionary into DataFramedf = pd.DataFrame(data)  # Declare a list that is to be converted into a columnaddress = ['Delhi', 'Bangalore', 'Chennai', 'Patna']  # Using 'Address' as the column name# and equating it to the listdf['Address'] = address  # Observe the resultprint(df)
Output:For more examples refer to Adding new column to existing DataFrame in Pandas Column Deletion:In Order to delete a column in Pandas DataFrame, we can use the drop() method. Columns is deleted by dropping columns with column names.
# importing pandas moduleimport pandas as pd  # making data frame from csv filedata = pd.read_csv("nba.csv", index_col ="Name" )  # dropping passed columnsdata.drop(["Team", "Weight"], axis = 1, inplace = True)  # displayprint(data)
Output:As shown in the output images, the new output doesn’t have the passed columns. Those values were dropped since axis was set equal to 1 and the changes were made in the original data frame since inplace was True.Data Frame before Dropping Columns-Data Frame after Dropping Columns-For more examples refer to Delete columns from DataFrame using Pandas.drop()
In order to deal with rows, we can perform basic operations on rows like selecting, deleting, adding and renaming.
Row Selection:Pandas provide a unique method to retrieve rows from a Data frame.DataFrame.loc[] method is used to retrieve rows from Pandas DataFrame. Rows can also be selected by passing integer location to an iloc[] function.
# importing pandas packageimport pandas as pd  # making data frame from csv filedata = pd.read_csv("nba.csv", index_col ="Name")  # retrieving row by loc methodfirst = data.loc["Avery Bradley"]second = data.loc["R.J. Hunter"]    print(first, "\n\n\n", second)
Output:As shown in the output image, two series were returned since there was only one parameter both of the times.For more examples refer to Pandas Extracting rows using .loc[] Row Addition:In Order to add a Row in Pandas DataFrame, we can concat the old dataframe with new one.
# 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 dataframesdf = pd.concat([new_row, df]).reset_index(drop = True)df.head(5)
Output:Data Frame before Adding Row-Data Frame after Adding Row-For more examples refer to Add a row at top in pandas DataFrame Row Deletion:In Order to delete a row in Pandas DataFrame, we can use the drop() method. Rows is deleted by dropping Rows by index label.
# importing pandas moduleimport pandas as pd  # making data frame from csv filedata = pd.read_csv("nba.csv", index_col ="Name" )  # dropping passed valuesdata.drop(["Avery Bradley", "John Holland", "R.J. Hunter",                            "R.J. Hunter"], inplace = True)  # displaydata
Output:As shown in the output images, the new output doesn’t have the passed values. Those values were dropped and the changes were made in the original data frame since inplace was True.Data Frame before Dropping values-Data Frame after Dropping values-For more examples refer to Delete rows from DataFrame using Pandas.drop() Problem related to Columns:
How to get column names in Pandas dataframe
How to rename columns in Pandas DataFrame
How to drop one or multiple columns in Pandas Dataframe
Get unique values from a column in Pandas DataFrame
How to lowercase column names in Pandas dataframe
Apply uppercase to a column in Pandas dataframe
Capitalize first letter of a column in Pandas dataframe
Get n-largest values from a particular column in Pandas DataFrame
Get n-smallest values from a particular column in Pandas DataFrame
Convert a column to row name/index in Pandas
Problem related to Rows:
Apply function to every row in a Pandas DataFrame
How to get rows names in Pandas dataframe
puja84375
Python pandas-dataFrame
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python String | replace()