Day 16 : Pandas Basics

Hey guys,

previously we learned about turtle graphics design.

Today we will learn about the library pandas which is used for data manipulation and cleaning.

What is pandas? 
NO not forest or zoo panda.
Pandas is a high-level data manipulation tool developed by Wes McKinney. It is built on the Numpy package and its key data structure is called the DataFrame. DataFrames allow you to store and manipulate tabular data in rows of observations and columns of variables.


Why Pandas are used?
Pandas is used for data manipulation, analysis, and cleaning. Python pandas is well suited for different kinds of data, such as Tabular data with heterogeneously-typed columns. Ordered and unordered time series data.


How to use Pandas?
Pandas is not built-in library so you have to install it.

pip install pandas


Let's play with pandas

How to import?
>>> import pandas as pd
Same as we import all other libraries


Code:
>>> dic={"name":["Mephisto","Lucifer","Galactus"],
     "age":[30,40,54]}
>>> dic
{'name': ['Mephisto', 'Lucifer', 'Galactus'], 'age': [30, 40, 54]}
>>> import pandas as pd
>>> df=pd.DataFrame(dic)
>>> df
       name  age
0  Mephisto   30
1   Lucifer   40
2  Galactus   54


Explain:
dic is dictionary of name and age column
Dataframe is a Two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns).


How to read data from file?
name = pd.read_csv('name.csv')


Indexing Dataframes
There are several ways to index a Pandas DataFrame. One of the easiest ways to do this is by using square bracket notation.
In the example below, you can use square brackets to select one column of the cars DataFrame. You can either use a single bracket or a double bracket. The single bracket with output a Pandas Series, while a double bracket will output a Pandas DataFrame.
Code:
# Import pandas and cars.csv
import pandas as pd
cars = pd.read_csv('cars.csv', index_col = 0)

# Print out country column as Pandas Series
print(cars['cars_per_cap'])

# Print out country column as Pandas DataFrame
print(cars[['cars_per_cap']])

# Print out DataFrame with country and drives_right columns

print(cars[['cars_per_cap', 'country']])



Square brackets can also be used to access observations (rows) from a DataFrame. For example:

code:
# Import cars data
import pandas as pd
cars = pd.read_csv('cars.csv', index_col = 0)

# Print out first 4 observations
print(cars[0:4]) 

# Print out fifth and sixth observation

print(cars[4:6])

Actually it work like list.



You can also use loc and iloc to perform just about any data selection operation. loc is label-based, which means that you have to specify rows and columns based on their row and column labels. iloc is integer index based, so you have to specify rows and columns by their integer index like you did in the previous exercise.

Code:
# Import cars data
import pandas as pd
cars = pd.read_csv('cars.csv', index_col = 0)

# Print out observation for Japan
print(cars.iloc[2])

# Print out observations for Australia and Egypt

print(cars.loc[['AUS', 'EG']])


Basically, you can perform lots of operation which you can do in Database or SQL.
Join and other operation Read Here

❤❤Quarantine python group link ❤❤

8805271377 WhatsApp

Follow here ❤

@mr._mephisto_ Instagram 

There will be no restrictions just feel free to learn. 

Share and take one more step to share knowledge to others. 

Believe in yourself 🤟 you are awesome. 

Be safe, Be happy😁
Take care of yourself and your family 
Of course watch movies and series🤟😉 

And follow the guidelines of government

Comments

Popular posts from this blog

News website using Flask and news API : Part 1

Web Design Challenge