Developer Snippet Diary

Pnadas liberary

Pandas is a Python library used for analyzing, cleaning, exploring, and manipulating data.. To install

C:\Users\Your Name>pip install pandas

Check version:

import pandas as pd
print(pd.__version__)

A Pandas Series is like a column in a table.

import pandas as pd
a = [1, 7, 2]
myvar = pd.Series(a)
print(myvar)
print(myvar.iloc[0]) #1
import pandas as pd
calories = {"day1": 420, "day2": 380, "day3": 390} #The keys of the dictionary become the labels.
myvar = pd.Series(calories)
print(myvar)

A Pandas DataFrame is a 2 dimensional data structure, like a 2 dimensional array, or a table with rows and columns.

import pandas as pd
data = {
      "name": ["apple","kiwi", "Banana"],
      "calories": [420, 380, 390],
      "duration": [50, 40, 45],
    }
    df = pd.DataFrame(data) #load data into a DataFrame object:
   df.loc[0,"'newColumn'"] ="Hello" # add a new column newColumn and set first value to Hello
   df['newColumn'] = None # A new colum added with all None values

    print(df.loc[0])  #return apple, 420,50
    print(df.loc[[0,1]])  #return only 0,1 row
READ CSV:
Pandas will only return the first 5 rows, and the last 5 rows:
import pandas as pd
df = pd.read_csv('data.csv') # Dataframe
print(df.to_string())  #datafram to entire string

print(df.head(10)) # with header return 10 rows
print(df.tail())  # last 5 rows 

   >>Max Rows Limit

print(pd.options.display.max_rows) #60 in mostly systems so if there are more than 60 rows in sheet it will return only first and last 5,5
pd.options.display.max_rows = 9999 # to increase default rows limit

JSON = Python DictionarY

df = pd.read_json('data.json')
print(df.to_string())  #use to_string() to print the entire DataFrame.

 

Posted by: R GONDAL
Email: rizikmw@gmail.com