Python’s Pandas dictionary-1 (Basics)

  1. Read data/Import Excel files:
    Most of the times we use .csv and .xlsx formats of Excel file, so discuss how to read or import those files here.
  2. a. Import CSV file:
import pandas as pd
df = pd.read_csv(r’My_folder\Pandas\file_name.csv’)

#[OR] here we use r’ ‘ for having ‘\’
df = pd.read_csv(‘My_folder/Pandas/file_name.csv’)
import pandas as pd
#for importing .xlsx file
df = pd.read_excel(r‘My_folder\Pandas\file_name.xlsx’)
#for importing .xls file
df = pd.read_excel(r‘My_folder\Pandas\file_name.xls’)
#a. Print top 5 rows of df
df.head() #[OR]
df[:5]
#b. Print last 5 rows of df
df.tail() #[OR]
df[5:]
df.info()
#If all the values are numeric without letters or special characters.df[‘column1’] = df[‘column1’].astype(‘int’)df[‘column1’] = df[‘column1’].astype(‘float’)(or)df[‘column1’] = pd.to_numeric(df[‘column1’])
df[‘column1’] = df[‘column1’].astype(‘object’)
df[‘column1’] = pd.to_datetime(df[‘Date’])
#To check mean (Average) of all numeric columns.
df.mean()
#To check mean of column.
df[‘Column_name’].mean()
#To check Median of all numeric columns.
df.median()
#To check Median of column.
df[‘Column_name’].median()
#To check Mode of all numeric columns.
df.mode()
#To check Mode of column.
df[‘Column_name’].mode()
#For all columns
df.describe()
#For selected columns.
df[[‘Column1’, ’Column2’, ‘Column3’]].describe()
#Drop columns:
df.drop([‘Column1’,’column2’], axis=1)
#Drop rows from index 1:10
df.drop(df.index[1:10])
#Or if you want to drop rows based on condition or a column categorydf.loc[df['column1'] != 'Blank'] #ORdf.loc[~df['column1'] == 'Blank']
df.isnull().sum()
df.isnull().sum()/len(df.index)

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

What’s LDV and Why Does it Matter for My Nonprofit or School? Part II

Ansible: Introduction and case studies

Top 6 Challenges in Software Migration for Startup

Introduction to Automated Testing using Selenium - GSoC ’18 Week 7

Introduction to Apache Heron

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Maniswaroop

Maniswaroop

More from Medium

Python Set Methods

R vs MatLab: The Key Difference Between Two Programming Languages

Python Basics, Variables & Data Types

Pandas