Pandas What Can Pandas Do For You ?: Statsmodels SM Seaborn Sns
Pandas What Can Pandas Do For You ?: Statsmodels SM Seaborn Sns
sets of tools in Python. In this exercise, we will examine two modules that are
frequently used by Data Scientists:
Pandas
CSV files:
Import pandas as pd
Df = pd.read_csv('random.csv')
Print(df)
Methods:
Print(Df.head()) (method to display the first five lines in data frame)
Dataframe name['columnname']
Dataframe name.columnname
Logical statements:
In data frame we can compare more than one variable with one variable
matplotlib
plt.xscale('log');
Making a bar chart:
Plt.bar(x, y)
Plt.barh(x,y) to make horizontal bar chart
Adding error bars:
Yerr=dataframename.error
Stacked bar carts:
Display two diff. sets of bars
Bottom = df.column
Making a histogram:
Plt.hist(x,y)
Changing bins:
Bins=num of bins
Changing range:
Range = (xmin, xmax)
Normalizing:
Reduces the height of each bar by a const. factor so that the area of each bar adda
to one.
Density=True
Dictionary:
varName = {key: value}
name[key] >> to print value
name.keys() >> print all keys
key in name >> true/ false
del(name[key]);
The DataFrame is one of Pandas' most important data structures. way to store
tabular data where you can label the rows and the columns. One way to build a
DataFrame is from a dictionary.
Import pandas as pd
To turn dictionary into dataframe
DataFrameName= pd.dataframe(dictionaryName);
dataframeName.index = …;
CSV files:
Name= pd.read_csv('', index_col = 0);
● Column access
● loc (label-based)
● Row access
● Column access
● Row & Column access brics[["country", "capital"]] brics[1:4] brics.loc[["RU", "IN", "CH"]]
brics.loc[:, ["country", "capital"]] brics.loc[["RU", "IN", "CH"], ["country", "capital"]]
2- iloc: position-based
Like loc but instead of col names you put numbers from 0 to …
Loops:
1- While loop = repeated if statement
Syntax:
While condition :
expression
2- For loop:
Syntax:
For var in seq :
Expression
Ex:
Fam = [1, 2, 4, 5]
For height in fam :
Print(height)
Using enumerate:
For index, height in enumerate (fam) :
Print( height + index)
For c in "family" :
The loop will run for a number of times equal to each char in the string
Print(val)
Print(row)
dataframeName["colName'].apply(function)
Random Number:
Random generators:
Np.random.rand()
Np.random.randint(0,2) generate 0 or 1
For x in range(number) : >> make for loop run for a number of times
Np.mean()
1-Defining a function:
Def funcName(): (function header)
Function body
2-Function Parameters:
Def funcName(value):
……
funcName(---)
Return ---
4-Docstrings:
-describe what your function does, placed in the immediate line after the function header
"""….."""
Even_nums = (2, 4, 6)
A, b, c = even_nums
Python first looks in local, then enclosing functions if there any, then global then built-in
scope
Using Global key word alter the value of a variable defined in the global scope.
Nested Functions:
Def outer(..):
Code
Def inner(…):
code
Returning Functions
Closure: anything defined locally in the enclosing scope is available to the inner
function even when the outer function has finished execution.
Using nonlocal to alter the value of a variable defined in the enclosing scope.
Lambda Functions:
Varname = lambda x, y: x ** y
Varname(1,2)
Anonymous functions:
Map(func, seq)
We can write this function into map using lambda and without defining it.
Try:
Print()
If x < 0:
Raise ValueError('')
Try:
….
Except:
Print()