• Latest
  • Trending
30 pandas Commands for Manipulating DataFrames

30 pandas Commands for Manipulating DataFrames

March 4, 2022
Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023
Vice President Dr. Bawumia inaugurates  ICT Hub

Vice President Dr. Bawumia inaugurates ICT Hub

April 2, 2023
Co-Creation Hub’s edtech accelerator puts $15M towards African startups

Co-Creation Hub’s edtech accelerator puts $15M towards African startups

February 20, 2023
Data Leak Hits Thousands of NHS Workers

Data Leak Hits Thousands of NHS Workers

February 20, 2023
EU Cybersecurity Agency Warns Against Chinese APTs

EU Cybersecurity Agency Warns Against Chinese APTs

February 20, 2023
How Your Storage System Will Still Be Viable in 5 Years’ Time?

How Your Storage System Will Still Be Viable in 5 Years’ Time?

February 20, 2023
The Broken Promises From Cybersecurity Vendors

Cloud Infrastructure Used By WIP26 For Espionage Attacks on Telcos

February 20, 2023
Instagram and Facebook to get paid-for verification

Instagram and Facebook to get paid-for verification

February 20, 2023
YouTube CEO Susan Wojcicki steps down after nine years

YouTube CEO Susan Wojcicki steps down after nine years

February 20, 2023
Inaugural AfCFTA Conference on Women and Youth in Trade

Inaugural AfCFTA Conference on Women and Youth in Trade

September 6, 2022
  • Consumer Watch
  • Kids Page
  • Directory
  • Events
  • Reviews
Saturday, 2 May, 2026
  • Login
itechnewsonline.com
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion
Subscription
Advertise
No Result
View All Result
itechnewsonline.com
No Result
View All Result

30 pandas Commands for Manipulating DataFrames

Read and write data to Excel sheets, modify DataFrames in one line of code, remove all rows containing null values... you can do it all with pandas.

by ITECHNEWS
March 4, 2022
in Data Science, Leading Stories
0 0
0
30 pandas Commands for Manipulating DataFrames

The pandas library makes python-based data science an easy ride. It’s a popular Python library for reading, merging, sorting, cleaning data, and more. Although pandas is easy to use and apply on datasets, it has many data manipulatory functions to learn.

You might use pandas, but there’s a good chance you’re under-utilizing it to solve data-related problems. Here’s our list of valuable data manipulating pandas functions every data scientist should know.

YOU MAY ALSO LIKE

French Telco Orange Hit by Cyber-Attack

ATC Ghana supports Girls-In-ICT Program

Install pandas Into Your Virtual Environment

Before we proceed, make sure you install pandas into your virtual environment using pip:

pip install pandas

After installing it, import pandas at the top of your script, and let’s proceed.

1. pandas.DataFrame

You use pandas.DataFrame() to create a DataFrame in pandas. There are two ways to use this function.

You can form a DataFrame column-wise by passing a dictionary into the pandas.DataFrame()function. Here, each key is a column, while the values are the rows:

import pandas
DataFrame = pandas.DataFrame({"A" : [1, 3, 4], "B": [5, 9, 12]})
print(DataFrame)

The other method is to form the DataFrame across rows. But here, you’ll separate the values (row items) from the columns. The number of data in each list (row data) must also tally with the number of columns.

import pandas
DataFrame = pandas.DataFrame([[1, 4, 5], [7, 19, 13]], columns= ["J", "K", "L"])
print(DataFrame)

2. Read From and Write to Excel or CSV in pandas

You can read or write to Excel or CSV files with pandas.

Reading Excel or CSV files

To read an Excel file:

#Replace example.xlsx with the your Excel file path 
DataFrame = DataFrame.read_excel("example.xlsx")

Here’s how to read a CSV file:

#Replace example.csv with the your CSV file path 
DataFrame = DataFrame.read_csv("example.csv")

Writing to Excel or CSV

Writing to Excel or CSV is a well-known pandas operation. And it’s handy for saving newly computed tables into separate datasheets.

To write to an Excel sheet:

DataFrame.to_excel("full_path_of_the_destination_folder/filename.xlsx")

If you want to write to CSV:

DataFrame.to_csv("full_path_of_the_destination_folder/filename.csv")

3. Get the Mean, Median, and Mode

You can also compute the central tendencies of each column in a DataFrame using pandas.

Here’s how to get the mean value of each column:

DataFrame.mean()

For the median or mode value, replace mean() with median() or mode().

4. DataFrame.transform

pandas’ DataFrame.transform() modifies the values of a DataFrame. It accepts a function as an argument.

For instance, the code below multiplies each value in a DataFrame by three using Python’s lambda function:

DataFrame = DataFrame.transform(lambda y: y*3)
print(DataFrame)

5. DataFrame.isnull

This function returns a Boolean value and flags all rows containing null values as True:

DataFrame.isnull()

The result of the above code can be hard to read for larger datasets. So you can use the isnull().sum() function instead. This returns a summary of all missing values for each column:

DataFrame.isnull().sum()

6. Dataframe.info

The info() function is an essential pandas operation. It returns the summary of non-missing values for each column instead:

DataFrame.info()

7. DataFrame.describe

The describe() function gives you the summary statistic of a DataFrame:

DataFrame.describe()

8. DataFrame.replace

Using the DataFrame.replace() method in pandas, you can replace selected rows with other values.

For example, to swap invalid rows with Nan:

# Ensure that you pip install numpy for this to work 
import numpy
import pandas
# Adding an inplace keyword and setting it to True makes the changes permanent: 
DataFrame.replace([invalid_1, invalid_2], numpy.nan, inplace=True)
print(DataFrame)

9. DataFrame.fillna

This function lets you fill empty rows with a particular value. You can fill all Nan rows in a dataset with the mean value, for instance:

DataFrame.fillna(df.mean(), inplace = True)
print(DataFrame)

You can also be column-specific:

DataFrame['column_name'].fillna(df[column_name].mean(), inplace = True)
print(DataFrame)

10. DataFrame.dropna

The dropna() method removes all rows containing null values:

DataFrame.dropna(inplace = True)
print(DataFrame)

11. DataFrame.insert

You can use pandas’ insert() function to add a new column to a DataFrame. It accepts three keywords, the column name, a list of its data, and its location, which is a column index.

Here’s how that works:

DataFrame.insert(column = 'C', value = [3, 4, 6, 7], loc=0)
print(DataFrame)

The above code inserts the new column at the zero column index (it becomes the first column).

12. DataFrame.loc

You can use loc to find the elements in a particular index. To view all items in the third row, for instance:

DataFrame.loc[2]

13. DataFrame.pop

This function lets you remove a specified column from a pandas DataFrame.

It accepts an item keyword, returns the popped column, and separates it from the rest of the DataFrame:

DataFrame.pop(item= 'column_name')
print(DataFrame)

14. DataFrame.max, min

Getting the maximum and minimum values using pandas is easy:

DataFrame.min()

The above code returns the minimum value for each column. To get the maximum, replace min with max.

15. DataFrame.join

The join() function of pandas lets you merge DataFrames with different column names. You can use the left, right, inner, or outer join. To left-join a DataFrame with two others:

#Left-join longer columns with shorter ones
newDataFrame = df1.join([df_shorter2, df_shorter3], how='left') 
print(newDataFrame)

To join DataFrames with similar column names, you can differentiate them by including a suffix to the left or right. Do this by including the lsuffix or rsuffix keyword:

newDataFrame = df1.join([df2, rsuffix='_', how='outer') 
print(newDataFrame)

16. DataFrame.combine

The combine() function comes in handy for merging two DataFrames containing similar column names based on set criteria. It accepts a function keyword.

For instance, to merge two DataFrames with similar column names based on the maximum values only:

newDataFrame = df.combine(df2, numpy.minimum)
print(newDataFrame)

Note: You can also define a custom selection function and insert numpy.minimum.

17. DataFrame.astype

The astype() function changes the data type of a particular column or DataFrame.

To change all values in a DataFrame to string, for instance:

DataFrame.astype(str)

18. DataFrame.sum

The sum() function in pandas returns the sum of the values in each column:

DataFrame.sum()

You can also find the cumulative sum of all items using cumsum():

DataFrame.cumsum()

19. DataFrame.drop

pandas’ drop() function deletes specific rows or columns in a DataFrame. You have to supply the column names or row index and an axis to use it.

To remove specific columns, for example:

df.drop(columns=['colum1', 'column2'], axis=0)

To drop rows on indexes 1, 3, and 4, for instance:

df.drop([1, 3, 4], axis=0)

20. DataFrame.corr

Want to find the correlation between integer or float columns? pandas can help you achieve that using the corr() function:

DataFrame.corr()

The above code returns a new DataFrame containing the correlation sequence between all integer or float columns.

21. DataFrame.add

The add() function lets you add a specific number to each value in DataFrame. It works by iterating through a DataFrame and operating on each item.

 

To add 20 to each of the values in a specific column containing integers or floats, for instance:

DataFrame['interger_column'].add(20)

22. DataFrame.sub

Like the addition function, you can also subtract a number from each value in a DataFrame or specific column:

DataFrame['interger_column'].sub(10)

23. DataFrame.mul

This is a multiplication version of the addition function of pandas:

DataFrame['interger_column'].mul(20)

24. DataFrame.div

Similarly, you can divide each data point in a column or DataFrame by a specific number:

DataFrame['interger_column'].div(20)

25. DataFrame.std

Using the std() function, pandas also lets you compute the standard deviation for each column in a DataFrame. It works by iterating through each column in a dataset and calculating the standard deviation for each:

DataFrame.std()

26. DataFrame.sort_values

You can also sort values ascendingly or descendingly based on a particular column. To sort a DataFrame in descending order, for example:

newDataFrame = DataFrame.sort_values(by = "colmun_name", descending = True)

27. DataFrame.melt

The melt() function in pandas flips the columns in a DataFrame to individual rows. It’s like exposing the anatomy of a DataFrame. So it lets you view the value assigned to each column explicitly.

newDataFrame = DataFrame.melt()

28. DataFrame.count

This function returns the total number of items in each column:

DataFrame.count()

29. DataFrame.query

pandas’ query() lets you call items using their index number. To get the items in the third row, for example:

DataFrame.query('4') # Call the query on the fourth index 

30. DataFrame.where

The where() function is a pandas query that accepts a condition for getting specific values in a column. For instance, to get all ages less than 30 from an Age column:

DataFrame.where(DataFrame['Age'] < 30)

The above code outputs a DataFrame containing all ages less than 30 but assigns Nan to rows that don’t meet the condition. ​​​

Handle Data Like a Pro With pandas

pandas is a treasure trove of functions and methods for handling small to large-scale datasets with Python. The library also comes in handy for cleaning, validating, and preparing data for analysis or machine learning.

Source: IDOWU OMISOLA
Via: makeuseof
Tags: 30 pandas Commands
ShareTweet

Get real time update about this post categories directly on your device, subscribe now.

Unsubscribe

Search

No Result
View All Result

Recent News

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023

About What We Do

itechnewsonline.com

We bring you the best Premium Tech News.

Recent News With Image

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025

Recent News

  • Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa July 29, 2025
  • French Telco Orange Hit by Cyber-Attack July 29, 2025
  • ATC Ghana supports Girls-In-ICT Program April 25, 2023
  • Vice President Dr. Bawumia inaugurates ICT Hub April 2, 2023
  • Home
  • InfoSec
  • Opinion
  • Africa Tech
  • Data Storage

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In

Add New Playlist

No Result
View All Result
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
Go to mobile version