Python | Creating a Pandas dataframe column based on a given condition
Last Updated :
11 Jul, 2024
While operating on data, there could be instances where we would like to add a column based on some condition. There does not exist any library function to achieve this task directly, so we are going to see how we can achieve this goal. In this article, we will see how to create a Pandas dataframe column based on a given condition in Python.
Problem: Given a Dataframe containing the data of a cultural event, add a column called 'Price' which contains the ticket price for a particular day based on the type of event that will be conducted on that particular day.
Creating a Pandas Dataframe Column Based on a condition
There are various methods to Create a Pandas Dataframe Column Based on a Given Condition here we are explaining some generally used methods for Creating a Pandas dataframe column based on a given condition.
Creating a DataFrame
Here we are creating the data frame to solve the given problem.
Python
# importing pandas as pd
import pandas as pd
# Creating the dataframe
df = pd.DataFrame({'Date': ['11/8/2011', '11/9/2011', '11/10/2011',
'11/11/2011', '11/12/2011'],
'Event': ['Music', 'Poetry', 'Music', 'Comedy', 'Poetry']})
# Print the dataframe
print(df)
Output
Date Event
0 11/8/2011 Music
1 11/9/2011 Poetry
2 11/10/2011 Music
3 11/11/2011 Comedy
4 11/12/2011 Poetry
Using List Comprehension
We can use Python's list comprehension technique to achieve this task. List comprehension is mostly faster than other methods.
Now we will add a new column called 'Price' to the dataframe. For that purpose, we will use list comprehension technique. Set the price to 1500 if the 'Event' is 'Music' else 800.
Python
# Add a new column named 'Price'
df['Price'] = [1500 if x == 'Music' else 800 for x in df['Event']]
# Print the DataFrame
print(df)
Output :
Date Event Price
0 11/8/2011 Music 1500
1 11/9/2011 Poetry 800
2 11/10/2011 Music 1500
3 11/11/2011 Comedy 800
4 11/12/2011 Poetry 800
As we can see in the output, we have successfully added a new column to the dataframe based on some condition.
Using DataFrame.apply() Function
We can use DataFrame.apply() function to achieve the goal. There could be instances when we have more than two values, in that case, we can use a dictionary to map new values onto the keys. This does provide a lot of flexibility when we are having a larger number of categories for which we want to assign different values to the newly added column.
Now we will add a new column called 'Price' to the dataframe. For that purpose we will use DataFrame.apply() function to achieve the goal. Set the price to 1500 if the 'Event' is 'Music', 1200 if the 'Event' is 'Comedy' and 800 if the 'Event' is 'Poetry'.
Python
# Define a function to map the values
def set_value(row_number, assigned_value):
return assigned_value[row_number]
# Create the dictionary
event_dictionary = {'Music': 1500, 'Poetry': 800, 'Comedy': 1200}
# Add a new column named 'Price'
df['Price'] = df['Event'].apply(set_value, args=(event_dictionary, ))
# Print the DataFrame
print(df)
Output :
Date Event Price
0 11/8/2011 Music 1500
1 11/9/2011 Poetry 800
2 11/10/2011 Music 1500
3 11/11/2011 Comedy 1200
4 11/12/2011 Poetry 800
As we can see in the output, we have successfully added a new column to the dataframe based on some condition.
Using DataFrame.map() Function
We can use DataFrame.map() function to achieve the goal. It is a very straight forward method where we use a dictionary to simply map values to the newly added column based on the key.
Now we will add a new column called 'Price' to the dataframe. For that purpose we will use DataFrame.map() function to achieve the goal. Set the price to 1500 if the 'Event' is 'Music', 1200 if the 'Event' is 'Comedy' and 800 if the 'Event' is 'Poetry'.
Python
# Create the dictionary
event_dictionary ={'Music' : 1500, 'Poetry' : 800, 'Comedy' : 1200}
# Add a new column named 'Price'
df['Price'] = df['Event'].map(event_dictionary)
# Print the DataFrame
print(df)
Output :
Date Event Price
0 11/8/2011 Music 1500
1 11/9/2011 Poetry 800
2 11/10/2011 Music 1500
3 11/11/2011 Comedy 1200
4 11/12/2011 Poetry 800
Using numpy.where() Function
We can use numpy.where() function to achieve the goal. It is a very straight forward method where we use a where condition to simply map values to the newly added column based on the condition.
Now we will add a new column called 'Price' to the dataframe. Set the price to 1500 if the 'Event' is 'Music', 1500 and rest all the events to 800.
Python
# np.where(condition, value if condition
# is true, value if condition is false)
df['Price'] = np.where(df['Event']
=='Music', 1500,800 )
print(df)
Output
Date Event Price
0 11/8/2011 Music 1500
1 11/9/2011 Poetry 800
2 11/10/2011 Music 1500
3 11/11/2011 Comedy 800
4 11/12/2011 Poetry 800
Using Assign() function
We use assign()
function in Pandas to assign new columns to a DataFrame. It returns a new DataFrame with the new columns added. You can use this method to create a new column based on a given condition.
Let's use the above DataFrame and modify the code to create a new column 'Category' based on the 'Event' column. In this example, we'll create a 'Category' column with values 'Entertainment' for 'Music' and 'Comedy' events, and 'Literature' for 'Poetry' events:
Python
# Define a condition to create a new column
# Let's say 'Entertainment' for 'Music' and 'Comedy', and 'Literature' for 'Poetry'
condition = df['Event'].isin(['Music', 'Comedy'])
df = df.assign(Category=lambda x: 'Entertainment' if condition.any() else 'Literature')
# Display the updated DataFrame
print(df)
Output :
Date Event Category
0 11/8/2011 Music Entertainment
1 11/9/2011 Poetry Literature
2 11/10/2011 Music Entertainment
3 11/11/2011 Comedy Entertainment
4 11/12/2011 Poetry Literature
Using DataFrame.loc[] function
We use DataFrame.loc[]
function in pandas to access a group of rows and columns by labels or a boolean array. It's a powerful tool for conditional indexing and updating values in a DataFrame. Let's use it to create a new column based on a given condition.
Let's use the above DataFrame and modify the code in below code, we'll create a new column called 'Genre' based on the 'Event' column. If the 'Event' is 'Music', the 'Genre' will be set to 'Rock'; if it's 'Poetry', the 'Genre' will be set to 'Literary'; otherwise, it will be set to 'Other'.
Python
# Creating a new column 'Genre' based on a condition
df['Genre'] = 'Other' # Initialize the 'Genre' column with 'Other' as the default value
# Using DataFrame.loc[] to set values based on the condition
df.loc[df['Event'] == 'Music', 'Genre'] = 'Rock'
df.loc[df['Event'] == 'Poetry', 'Genre'] = 'Literary'
# Displaying the modified dataframe
print(df)
Output :
Date Event Genre
0 11/8/2011 Music Rock
1 11/9/2011 Poetry Literary
2 11/10/2011 Music Rock
3 11/11/2011 Comedy Other
4 11/12/2011 Poetry Literary
Using lambda function
A lambda function in Python is a concise, anonymous function created with the lambda
keyword, typically used for short operations.
In this example, a new column 'Priority' is created based on the condition: if the 'Event' is 'Music', the priority is set to 'High', otherwise, it's set to 'Low'. The apply()
function along with a lambda function is used to achieve this
Python
df = pd.DataFrame({'Date': ['11/8/2011', '11/9/2011', '11/10/2011', '11/11/2011', '11/12/2011'],
'Event': ['Music', 'Poetry', 'Music', 'Comedy', 'Poetry']})
# Adding a new column 'Priority' based on a condition using lambda function
df['Priority'] = df['Event'].apply(lambda x: 'High' if x == 'Music' else 'Low')
# Print the modified dataframe
print(df)
Output :
Date Event Priority
0 11/8/2011 Music High
1 11/9/2011 Poetry Low
2 11/10/2011 Music High
3 11/11/2011 Comedy Low
4 11/12/2011 Poetry Low
Similar Reads
Pandas Exercises and Programs Pandas is an open-source Python Library that is made mainly for working with relational or labelled data both easily and intuitively. This Python library is built on top of the NumPy library, providing various operations and data structures for manipulating numerical data and time series. Pandas is
6 min read
Different ways to create Pandas Dataframe It is the most commonly used Pandas object. The pd.DataFrame() function is used to create a DataFrame in Pandas. There are several ways to create a Pandas Dataframe in Python.Example: Creating a DataFrame from a DictionaryPythonimport pandas as pd # initialize data of lists. data = {'Name': ['Tom',
7 min read
Pandas DataFrame Practice Exercises
Create a Pandas DataFrame from ListsConverting lists to DataFrames is crucial in data analysis, Pandas enabling you to perform sophisticated data manipulations and analyses with ease. List to Dataframe Example# Simple listdata = [1, 2, 3, 4, 5]# Convert to DataFramedf = pd.DataFrame(data, columns=['Numbers'])Here we will discuss diffe
5 min read
Make a Pandas DataFrame with two-dimensional list | PythonIn this discussion, we will illustrate the process of creating a Pandas DataFrame with the two-dimensional list. Python is widely recognized for its effectiveness in data analysis, thanks to its robust ecosystem of data-centric packages. Among these packages, Pandas stands out, streamlining the impo
3 min read
Python | Creating DataFrame from dict of narray/listsAs we know Pandas is all-time great tools for data analysis. One of the most important data type is dataframe. It is a 2-dimensional labeled data structure with columns of potentially different types. It is generally the most commonly used pandas object. Pandas DataFrame can be created in multiple w
2 min read
Creating Pandas dataframe using list of listsIn this article, we will explore the Creating Pandas data frame using a list of lists. A Pandas DataFrame is a versatile 2-dimensional labeled data structure with columns that can contain different data types. It is widely utilized as one of the most common objects in the Pandas library. There are v
4 min read
Creating a Pandas dataframe using list of tuplesA Pandas DataFrame is an important data structure used for organizing and analyzing data in Python. Converting a list of tuples into a DataFrame makes it easier to work with data. In this article we'll see ways to create a DataFrame from a list of tuples.1. Using pd.DataFrame()The simplest method to
2 min read
Create a Pandas DataFrame from List of DictsPandas DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. It is generally the most commonly used Pandas object. Pandas DataFrame can be created in multiple ways using Python. Letâs discuss how to create a Pandas DataFrame from the List of Dictionaries. C
3 min read
Python | Convert list of nested dictionary into Pandas dataframeGiven a list of the nested dictionary, write a Python program to create a Pandas dataframe using it. We can convert list of nested dictionary into Pandas DataFrame. Let's understand the stepwise procedure to create a Pandas Dataframe using the list of nested dictionary. Convert Nested List of Dictio
4 min read
Replace values in Pandas dataframe using regexWhile working with large sets of data, it often contains text data and in many cases, those texts are not pretty at all. The text is often in very messier form and we need to clean those data before we can do anything meaningful with that text data. Mostly the text corpus is so large that we cannot
4 min read
Creating a dataframe from Pandas seriesSeries is a type of list in Pandas that can take integer values, string values, double values, and more. But in Pandas Series we return an object in the form of a list, having an index starting from 0 to n, Where n is the length of values in the series. Later in this article, we will discuss Datafra
5 min read
Construct a DataFrame in Pandas using string dataData comes in various formats and string data is one of the most common formats encountered when working with data sources such as CSV files, web scraping, or APIs. In this article, we will explore different ways to load string data into a Pandas DataFrame efficiently.Using StringIO()One way to crea
5 min read
Clean the string data in the given Pandas DataframeIn today's world data analytics is being used by all sorts of companies out there. While working with data, we can come across any sort of problem which requires an out-of-the-box approach for evaluation. Most of the Data in real life contains the name of entities or other nouns. It might be possibl
3 min read
Reindexing in Pandas DataFrameReindexing in Pandas is used to change the row or column labels of a DataFrame to match a new set of indices. This is useful when aligning data, adding missing labels, or reshaping your DataFrame. If the new index includes values not present in the original DataFrame, Pandas fills those with NaN by
4 min read
Mapping external values to dataframe values in PandasMapping external values to a dataframe means using different sets of values to add to that dataframe by keeping the keys of the external dictionary as same as the one column of that dataframe. To add external values to dataframe, we use a dictionary that has keys and values which we want to add to t
3 min read
Reshape a Pandas DataFrame using stack,unstack and melt methodPandas use various methods to reshape the dataframe and series. Reshaping a Pandas DataFrame is a common operation to transform data structures for better analysis and visualization. The stack method pivots columns into rows, creating a multi-level index Series. Conversely, the unstack method revers
5 min read
Reset Index in Pandas DataframeLetâs discuss how to reset the index in Pandas DataFrame. Often We start with a huge data frame in Pandas and after manipulating/filtering the data frame, we end up with a much smaller data frame. When we look at the smaller data frame, it might still carry the row index of the original data frame.
6 min read
Change column names and row indexes in Pandas DataFrameGiven a Pandas DataFrame, let's see how to change its column names and row indexes. About Pandas DataFramePandas DataFrame are rectangular grids which are used to store data. It is easy to visualize and work with data when stored in dataFrame. It consists of rows and columns.Each row is a measuremen
4 min read
How to print an entire Pandas DataFrame in Python?When we use a print large number of a dataset then it truncates. In this article, we are going to see how to print the entire Pandas Dataframe or Series without Truncation. There are 4 methods to Print the entire Dataframe. Example # Convert the whole dataframe as a string and displaydisplay(df.to_s
4 min read
Working with Missing Data in PandasIn Pandas, missing data occurs when some values are missing or not collected properly and these missing values are represented as:None: A Python object used to represent missing values in object-type arrays.NaN: A special floating-point value from NumPy which is recognized by all systems that use IE
5 min read
Pandas Dataframe Rows Practice Exercise
Efficient methods to iterate rows in Pandas DataframeWhen iterating over rows in a Pandas DataFrame, the method you choose can greatly impact performance. Avoid traditional row iteration methods like for loops or .iterrows() when performance matters. Instead, use methods like vectorization or itertuples(). Vectorized operations are the fastest and mos
5 min read
Different ways to iterate over rows in Pandas DataframeIterating over rows in a Pandas DataFrame allows to access row-wise data for operations like filtering or transformation. The most common methods include iterrows(), itertuples(), and apply(). However, iteration can be slow for large datasets, so vectorized operations are often preferred. Let's unde
5 min read
Selecting rows in pandas DataFrame based on conditionsLetâs see how to Select rows based on some conditions in Pandas DataFrame. Selecting rows based on particular column value using '>', '=', '=', '<=', '!=' operator. Code #1 : Selecting all the rows from the given dataframe in which 'Percentage' is greater than 80 using basic method. Python# im
6 min read
Select any row from a Dataframe using iloc[] and iat[] in PandasIn this article, we will learn how to get the rows from a dataframe as a list, using the functions ilic[] and iat[]. There are multiple ways to do get the rows as a list from given dataframe. Letâs see them will the help of examples. Python import pandas as pd # Create the dataframe df = pd.DataFram
2 min read
Limited rows selection with given column in Pandas | PythonMethods in Pandas like iloc[], iat[] are generally used to select the data from a given dataframe. In this article, we will learn how to select the limited rows with given columns with the help of these methods. Example 1: Select two columns Python3 # Import pandas package import pandas as pd # Defi
2 min read
Drop rows from dataframe based on certain condition applied on a column - PandasIn this post, we are going to discuss several approaches on how to drop rows from the dataframe based on certain conditions applied to a column. Whenever we need to eliminate irrelevant or invalid data, the primary way to do this is: boolean indexing which involves applying a condition to a DataFram
4 min read
Insert row at given position in Pandas DataframeInserting a row in Pandas DataFrame is a very straight forward process and we have already discussed approaches in how insert rows at the start of the Dataframe. Now, let's discuss the ways in which we can insert a row at any position in the dataframe having integer based index.Solution #1 : There d
3 min read
Create a list from rows in Pandas dataframePython lists are one of the most versatile data structures, offering a range of built-in functions for efficient data manipulation. When working with Pandas, we often need to extract entire rows from a DataFrame and store them in a list for further processing. Unlike columns, which are easily access
4 min read
Create a list from rows in Pandas DataFrame | Set 2In an earlier post, we had discussed some approaches to extract the rows of the dataframe as a Python's list. In this post, we will see some more methods to achieve that goal. Note : For link to the CSV file used in the code, click here. Solution #1: In order to access the data of each row of the Pa
2 min read
Ranking Rows of Pandas DataFrameTo rank the rows of Pandas DataFrame we can use the DataFrame.rank() method which returns a rank of every respective index of a series passed. The rank is returned on the basis of position after sorting. Example #1 : Here we will create a DataFrame of movies and rank them based on their ratings. Pyt
2 min read
Sorting rows in pandas DataFramePandas DataFrame is two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). We often need to do certain operations on both rows and column while handling the data. Letâs see how to sort rows in pandas DataFrame. Code #1: Sorting rows by Sc
2 min read
Select row with maximum and minimum value in Pandas dataframeLet's see how can we select rows with maximum and minimum values in Pandas Dataframe with help of different examples using Python. Creating a Dataframe to select rows with max and min values in DataframePython3 # importing pandas and numpy import pandas as pd import numpy as np # data of 2018 driver
2 min read
Get all rows in a Pandas DataFrame containing given substringLet's see how to get all rows in a Pandas DataFrame containing given substring with the help of different examples. Code #1: Check the values PG in column Position Python3 1== # importing pandas import pandas as pd # Creating the dataframe with dict of lists df = pd.DataFrame({'Name': ['Geeks', 'Pet
3 min read
Convert a column to row name/index in PandasPandas provide a convenient way to handle data and its transformation. Let's see how can we convert a column to row name/index in Pandas. Create a dataframe first with dict of lists. Python3 # importing pandas as pd import pandas as pd # Creating a dict of lists data = {'Name':["Akash", "Geeku", "
2 min read
How to Randomly Select rows from Pandas DataFrameIn Pandas, it is possible to select rows randomly from a DataFrame with different methods. Randomly selecting rows can be useful for tasks like sampling, testing or data exploration.Creating Sample Pandas DataFrameFirst, we will create a sample Pandas DataFrame that we will use further in our articl
3 min read
How to print an entire Pandas DataFrame in Python?When we use a print large number of a dataset then it truncates. In this article, we are going to see how to print the entire Pandas Dataframe or Series without Truncation. There are 4 methods to Print the entire Dataframe. Example # Convert the whole dataframe as a string and displaydisplay(df.to_s
4 min read
Pandas Dataframe Columns Practice Exercise
Create a pandas column using for loopLetâs see how to create a column in pandas dataframe using for loop. Such operation is needed sometimes when we need to process the data of dataframe created earlier for that purpose, we need this type of computation so we can process the existing data and make a separate column to store the data. I
2 min read
How to Get Column Names in Pandas DataframeWhile analyzing the real datasets which are often very huge in size, we might need to get the pandas column names in order to perform certain operations. The simplest way to get column names in Pandas is by using the .columns attribute of a DataFrame. Let's understand with a quick example:Pythonimpo
4 min read
How to rename columns in Pandas DataFrameIn this article, we will see how to rename column in Pandas DataFrame. The simplest way to rename columns in a Pandas DataFrame is to use the rename() function. This method allows renaming specific columns by passing a dictionary, where keys are the old column names and values are the new column nam
4 min read
Collapse multiple Columns in PandasWhile operating dataframes in Pandas, we might encounter a situation to collapse the columns. Let it be cumulated data of multiple columns or collapse based on some other requirement. Let's see how to collapse multiple columns in Pandas. Following steps are to be followed to collapse multiple column
2 min read
Get unique values from a column in Pandas DataFrameIn Pandas, retrieving unique values from DataFrame is used for analyzing categorical data or identifying duplicates. Let's learn how to get unique values from a column in Pandas DataFrame. Get the Unique Values of Pandas using unique()The.unique()method returns a NumPy array. It is useful for identi
5 min read
Conditional operation on Pandas DataFrame columnsSuppose you have an online store. The price of the products is updated frequently. While calculating the final price on the product, you check if the updated price is available or not. If not available then you use the last price available. Solution #1: We can use conditional expression to check if
4 min read
Return the Index label if some condition is satisfied over a column in Pandas DataframeGiven a Dataframe, return all those index labels for which some condition is satisfied over a specific column. Solution #1: We can use simple indexing operation to select all those values in the column which satisfies the given condition. Python3 # importing pandas as pd import pandas as pd # Creat
2 min read
Using dictionary to remap values in Pandas DataFrame columnsWhile working with data in Pandas, we often need to modify or transform values in specific columns. One common transformation is remapping values using a dictionary. This technique is useful when we need to replace categorical values with labels, abbreviations or numerical representations. In this a
4 min read
Formatting float column of Dataframe in PandasWhile presenting the data, showing the data in the required format is also a crucial part. Sometimes, the value is so big that we want to show only the desired part of this or we can say in some desired format. Let's see different methods of formatting integer columns and the data frame it in Pandas
3 min read
Create a New Column in Pandas DataFrame based on the Existing ColumnsWhen working with data in Pandas, we often need to change or organize the data into a format we want. One common task is adding new columns based on calculations or changes made to the existing columns in a DataFrame. In this article, we will be exploring different ways to do that.Task: We have a Da
4 min read
Python | Creating a Pandas dataframe column based on a given conditionWhile operating on data, there could be instances where we would like to add a column based on some condition. There does not exist any library function to achieve this task directly, so we are going to see how we can achieve this goal. In this article, we will see how to create a Pandas dataframe c
8 min read
Split a column in Pandas dataframe and get part of itWhen a part of any column in Dataframe is important and the need is to take it separate, we can split a column on the basis of the requirement. We can use Pandas .str accessor, it does fast vectorized string operations for Series and Dataframes and returns a string object. Pandas str accessor has nu
2 min read
Getting Unique values from a column in Pandas dataframeLet's see how can we retrieve the unique values from pandas dataframe. Let's create a dataframe from CSV file. We are using the past data of GDP from different countries. You can get the dataset from here. Python3 # import pandas as pd import pandas as pd gapminder_csv_url ='http://bit.ly/2cLzoxH' #
2 min read
Split a String into columns using regex in pandas DataFrameGiven some mixed data containing multiple values as a string, let's see how can we divide the strings using regex and make multiple columns in Pandas DataFrame. Method #1: In this method we will use re.search(pattern, string, flags=0). Here pattern refers to the pattern that we want to search. It ta
3 min read
Count Frequency of Columns in Pandas DataFrameWhen working with data in Pandas counting how often each value appears in a column is one of the first steps to explore our dataset. This helps you understand distribution of data and identify patterns. Now weâll explore various ways to calculate frequency counts for a column in a Pandas DataFrame.1
2 min read
Change Data Type for one or more columns in Pandas DataframeWhen working with data in Pandas working with right data types for your columns is important for accurate analysis and efficient processing. Pandas offers several simple ways to change or convert the data types of columns in a DataFrame. In this article, we'll look at different methods to help you e
3 min read
Split a text column into two columns in Pandas DataFrameLet's see how to split a text column into two columns in Pandas DataFrame. Method #1 : Using Series.str.split() functions. Split Name column into two different columns. By default splitting is done on the basis of single space by str.split() function. Python3 # import Pandas as pd import pandas as p
3 min read
Difference of two columns in Pandas dataframeDifference of two columns in pandas dataframe in Python is carried out by using following methods : Method #1 : Using â -â operator. Python3 import pandas as pd # Create a DataFrame df1 = { 'Name':['George','Andrea','micheal', 'maggie','Ravi','Xien','Jalpa'], 'score1':[62,47,55,74,32,77,86], 'score2
2 min read
Get the index of maximum value in DataFrame columnPandas DataFrame is two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). Let's see how can we get the index of maximum value in DataFrame column.Observe this dataset first. We'll use 'Weight' and 'Salary' columns of this data in order t
2 min read
Get the index of minimum value in DataFrame columnPandas DataFrame is two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). Let's see how can we get the index of minimum value in DataFrame column. Observe this dataset first. We'll use 'Weight' and 'Salary' columns of this data in order
2 min read
Get n-largest values from a particular column in Pandas DataFramePandas DataFrame is two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). Let's see how can we can get n-largest values from a particular column in Pandas DataFrame. Observe this dataset first. Weâll use âAgeâ, âWeightâ and âSalaryâ colu
1 min read
Get n-smallest values from a particular column in Pandas DataFramePandas DataFrame is two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). Let's see how can we can get n-smallest values from a particular column in Pandas DataFrame. Observe this dataset first. We'll use 'Age', 'Weight' and 'Salary' col
1 min read
How to drop one or multiple columns in Pandas DataFrameLet's learn how to drop one or more columns in Pandas DataFrame for data manipulation. Drop Columns Using df.drop() MethodLet's consider an example of the dataset (data) with three columns 'A', 'B', and 'C'. Now, to drop a single column, use the drop() method with the columnâs name.Pythonimport pand
4 min read
How to lowercase strings in a column in Pandas dataframeAnalyzing real-world data is somewhat difficult because we need to take various things into consideration. Apart from getting the useful data from large datasets, keeping data in required format is also very important. One might encounter a situation where we need to lowercase each letter in any spe
2 min read
Capitalize first letter of a column in Pandas dataframeAnalyzing real-world data is somewhat difficult because we need to take various things into consideration. Apart from getting the useful data from large datasets, keeping data in required format is also very important. One might encounter a situation where we need to capitalize any specific column i
2 min read
Apply uppercase to a column in Pandas dataframeAnalyzing a real world data is some what difficult because we need to take various things into consideration. Apart from getting the useful data from large datasets, keeping data in required format is also very important. One might encounter a situation where we need to uppercase each letter in any
2 min read
Pandas Series Practice Exercise
Create a Pandas Series from ArrayA Pandas Series is a one-dimensional labeled array that stores various data types, including numbers (integers or floats), strings, and Python objects. It is a fundamental data structure in the Pandas library used for efficient data manipulation and analysis. In this guide we will explore two simple
2 min read
Creating a Pandas Series from DictionaryA Pandas Series is a one-dimensional labeled array capable of holding any data type (integers, strings, floating point numbers, Python objects, etc.). It has to be remembered that, unlike Python lists, a Series will always contain data of the same type. Letâs see how to create a Pandas Series from P
2 min read
Creating a Pandas Series from ListsA Pandas Series is a one-dimensional labeled array capable of holding various data types such as integers, strings, floating-point numbers and Python objects. Unlike Python lists a Series ensures that all elements have the same data type. It is widely used in data manipulation and analysis.In this a
3 min read
Create Pandas Series using NumPy functionsA Pandas Series is like a one-dimensional array which can store numbers, text or other types of data. NumPy has built-in functions that generate sequences, random values or repeated numbers In this article, we'll learn how to create a Pandas Series using different functions from the NumPy library.Me
2 min read
Access the elements of a Series in PandasPandas Series is a one-dimensional labeled array capable of holding data of any type (integer, string, float, python objects, etc.). Labels need not be unique but must be a hashable type. Let's discuss different ways to access the elements of given Pandas Series. First create a Pandas Series. Python
2 min read
Pandas Date and Time Practice Exercise
DataFrame String Manipulation
Accessing and Manipulating Data in DataFrame
Access Index of Last Element in pandas DataFrame in PythonIn this article, we are going to see how to access an index of the last element in the pandas Dataframe. To achieve this, we can use Dataframe.iloc, Dataframe.iget, and Dataframe.index. let's go through all of them one by one.  Dataframe.iloc - Pandas Dataframe.iloc is used to retrieve data by spe
3 min read
Replace Characters in Strings in Pandas DataFrameIn this article, we are going to see how to replace characters in strings in pandas dataframe using Python. We can replace characters using str.replace() method is basically replacing an existing string or character in a string with a new one. we can replace characters in strings is for the entire
3 min read
Replace values of a DataFrame with the value of another DataFrame in PandasIn this article, we will learn how we can replace values of a DataFrame with the value of another DataFrame using pandas. It can be done using the DataFrame.replace() method. It is used to replace a regex, string,  list, series, number, dictionary, etc. from a DataFrame, Values of the DataFrame met
4 min read
Replace negative values with latest preceding positive value in Pandas DataFrameIn this article, we will discuss how to replace the negative value in Pandas DataFrame Column with the latest preceding positive value. While doing this there may arise two situations -Â Value remains unmodified if no proceeding positive value existsValue update to 0 if no proceeding positive value
3 min read
How to add column from another DataFrame in Pandas ?In this discussion, we will explore the process of adding a column from another data frame in Pandas. Pandas is a powerful data manipulation library for Python, offering versatile tools for handling and analyzing structured data. Add column from another DataFrame in Pandas There are various ways to
6 min read
DataFrame Visualization and Exporting