0% found this document useful (0 votes)
656 views

Top Pandas Functions

This document lists and describes 15 important functions in the pandas library for working with DataFrames. These functions include pd.read_csv() to read CSV files into a DataFrame, df.info() to display information about a DataFrame, and df.describe() to generate descriptive statistics. Other key functions are df.assign() to add columns, df.sample() to sample rows, df.head() and df.tail() to view rows, and df.drop() to remove rows or columns.

Uploaded by

ajit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
656 views

Top Pandas Functions

This document lists and describes 15 important functions in the pandas library for working with DataFrames. These functions include pd.read_csv() to read CSV files into a DataFrame, df.info() to display information about a DataFrame, and df.describe() to generate descriptive statistics. Other key functions are df.assign() to add columns, df.sample() to sample rows, df.head() and df.tail() to view rows, and df.drop() to remove rows or columns.

Uploaded by

ajit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

PANDAS

IMPORTANT
FUNCTIONS
TOP 15
IMPORTANT
PANDAS FUNCTIONS

https://www.linkedin.com/in/sriram-r-2a0420177
#1
pd.read_csv( )

pandas.read_csv() is used to read a CSV (Comma


Separated Values) file and convert it into a pandas
DataFrame.
CODE:

OUTPUT:

https://www.linkedin.com/in/sriram-r-2a0420177
#2
df.info()

df.info() is used to display a summary of a data frame,


including the data types and the number of non-null
values in each column.

CODE:

OUTPUT:

https://www.linkedin.com/in/sriram-r-2a0420177
#3
df.describe()

df.describe() method in Pandas is used to generate


descriptive statistics of the columns of a DataFrame.
useful for getting a quick overview of the distribution of the
data(mean, median, mode) and measures of dispersion
(standard deviation, range, interquartile range).

CODE:

OUTPUT:

https://www.linkedin.com/in/sriram-r-2a0420177
#4
df.assign()

df.assign() method in Pandas is used to add new columns


to a DataFrame.

CODE:

OUTPUT:

https://www.linkedin.com/in/sriram-r-2a0420177
#5
df.sample()

df.sample() function returns a random sample of rows


from a DataFrame.By default, it returns one random
row, but the number of rows can be specified as an
argument. For example, df.sample(5) returns 5 random
rows from the DataFrame. This function can be useful
for getting a quick understanding of the distribution of
values in a large dataset.

CODE:

OUTPUT:

https://www.linkedin.com/in/sriram-r-2a0420177
#6
df.head()

df.head() function returns the first n (default 5) rows of


a DataFrame.The n number of rows can be specified as
an argument, for example df.head(10) returns the first
10 rows of the DataFrame.
CODE:

OUTPUT:

https://www.linkedin.com/in/sriram-r-2a0420177
#7
df.tail()

The df.tail() function returns the last n (default 5) rows


of a DataFrame. The n number of rows can be specified
as an argument, for example df.tail(10) returns the last
10 rows of the DataFrame.

CODE:

OUTPUT:

https://www.linkedin.com/in/sriram-r-2a0420177
#8
df.drop( )

df.drop() method in pandas is used to remove rows or


columns from a data frame.

CODE:

OUTPUT: Customer_Age and Age_Group column is dropped from the data frame

https://www.linkedin.com/in/sriram-r-2a0420177
#9
df.dropna( )

df.dropna() method in Python is used to remove any rows


that contain missing values (i.e. NaN) from a DataFrame.
This can be useful for cleaning data before analysis or
modeling.
CODE:

OUTPUT:
Before After

https://www.linkedin.com/in/sriram-r-2a0420177
#10
df.query()

df.query() is used to filter rows of a DataFrame based


on a condition.

CODE:

OUTPUT:

https://www.linkedin.com/in/sriram-r-2a0420177
#11
df.sort_values()

df.sort_values() method in Pandas is used to sort the


rows of a DataFrame based on the values of columns.

CODE: CODE:

OUTPUT:

OUTPUT:

https://www.linkedin.com/in/sriram-r-2a0420177
#12
df.groupby().sum()

df.groupby() is used to group a data frame by one or


more columns. The result is a new data frame that has the
grouped columns as the index, and the other columns are
aggregated using a specified aggregation method.

CODE:

OUTPUT:

https://www.linkedin.com/in/sriram-r-2a0420177
#13
df.merge()

df.merge() is used to combine two or more DataFrames


into a single data frame. The function works by joining
the DataFrames on one or more common columns,
similar to a SQL JOIN operation

CODE:

OUTPUT:

https://www.linkedin.com/in/sriram-r-2a0420177
#14
df.rename()

df.rename() is used to rename one or more columns in


a data frame.

CODE:

OUTPUT:

https://www.linkedin.com/in/sriram-r-2a0420177
#15
df.to_csv()

df.to_csv() is used to save a data frame(export data) to


a CSV (Comma Separated Values) file.

CODE:

OUTPUT:

https://www.linkedin.com/in/sriram-r-2a0420177
By

https://www.linkedin.com/in/sriram-r-2a0420177

You might also like