Difference makes the DIFFERENCE
import numpy as np
import pandas as pd
mass = pd.Series([0.33, 4.87, 5.97, 0.642, 1898, 568, 86.8, 102, 0.0146], index=['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto'])
diameter = pd.Series([4879, 120104, 12756, 6792, 142984, 120536, 51118, 49528, 2376], index=['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto'])
arr = np.random.randint(1, 30, (5, 3))
arr
arr
# Creating a dataframe from the numpy array
df = pd.DataFrame(arr)
df
df.values
df.columns
df[2:3]
df.index
for c in df.columns:
print(c)
df.index = ["R1", "R2", "R3", "R4", "R5"]
df.columns = ["C1", "C2", "C3"]
df
df.loc['R3', 'C2']
df.iloc[2,1]
df.iloc[4,2]
df.iloc[2:4, 1:3]
type(df.iloc[2:4, 1:3])
arr
df.iloc[0]
type(df.iloc[0])
df.iloc[:2]
df.iloc[:1]
df.iloc[0:3]
df.iloc[:3]
df.iloc[:,0]
df.iloc[:,2]
df.iloc[2,:]
arr
df.loc["R2"]
df.shape
df.T
def create_df(nrows, ncols):
arr1 = np.random.randint(1, 30, (nrows, ncols))
df = pd.DataFrame(arr1)
print(arr1)
%%time
create_df(3, 4)
create_df(4, 5)
def createDF(nRows, nCols):
df = pd.DataFrame(np.random.randint(1, 30, (nRows, nCols)))
print(df)
%%time
createDF(4, 3)
createDF(10, 10)
import numpy as np
import pandas as pd
def create_DF(nRows, nCols, maxData=10):
arr = np.random.randint(0, maxData, (nRows, nCols))
df = pd.DataFrame(arr)
df.index = ["R" + str(x) for x in np.arange(1, nRows + 1)]
df.columns = ["C" + str(x) for x in np.arange(1, nCols + 1)]
return df
create_DF(4, 5, 50)
create_DF(2, 5)
mass = pd.Series([0.33, 4.87, 5.97, 0.642, 1898, 568, 86.8, 102, 0.0146],
index=['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto'])
diameter = pd.Series([4879, 120104, 12756, 6792, 142984, 120536, 51118, 49528, 2376],
index=['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto'])
mass
diameter
df = pd.DataFrame({'mass': mass, 'diameter':diameter})
df
diameter = diameter.append(pd.Series(3475, index=['Moon']))
diameter
df_moon = pd.DataFrame({'mass': mass, 'diameter':diameter})
df_moon
df['mass']
df['diameter']
df = pd.DataFrame({'mass': mass, 'diameter':diameter})
df['mass']
df['diameter']
df['mass']['Earth']
df.mass.Earth
df['diameter']['Earth']
df.diameter.Earth
df['Population'] = 0
df
df['Population']['Earth'] = 8000000000
df
df['pop'] = 0
df
df['mass'] == df.mass
df.mass is df['mass']
# returns series - horizontal
df.loc['Earth']
type(df.loc['Earth'])
# returns series - Vertical
df.loc[:, 'mass']
type(df.loc[:, 'mass'])
df['pop'] is df.pop
df2 = df
df2
new_row = {'mass':1000, 'diameter':50000,
'Population':0, 'pop':12345678}
df2.loc['New Row'] = new_row
df2
Mean_row = {'mass': np.mean(mass), 'diameter': np.mean(diameter)}
df2.loc['Mean_row'] = Mean_row
df2
df.loc['Col_Mean'] = 0
df
np.mean(df['mass'])
df.drop('Col_Mean')
df.drop('pop', axis=1)
df
df.drop('pop', axis = 1, inplace=True)
df
df.drop('Col_Mean', inplace = True)
df
df.drop('Population', axis = 1, inplace = True)
df
df.drop('Mean_row', inplace = True)
df.drop('New Row', inplace = True)
df
def create_mean_row(df):
df.loc['Col_Mean'] = [np.mean(df[col]) for col in df.columns]
return df
create_mean_row(df)
def create_New_Mean_Row(df):
df.loc['Col_Mean_row'] = df.mean()
return df
create_New_Mean_Row(df)
dff = pd.DataFrame(arr)
dff.mean()
dff.mean(axis = 1)
dff
dff['row_mean'] = dff.mean(axis = 1)
dff
dff
dff.loc['col_mean'] = dff.mean()
dff
df.median()
dff.loc['col_median'] = dff.median()
dff
dff['row_median'] = dff.median(axis = 1)
dff
dff['row_stddev'] = df.std(axis=1)
dff
dff.loc['col_stddev'] = dff.std()
dff
dff.quantile(0.25)
dff.min()
dff.max()
dff.describe()
df
mass = pd.Series(
[0.33, 4.87, 5.97, 0.642, 1898, 568, 86.8, 102, 0.0146],
index=['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto'])
diameter = pd.Series(
[4879, 120104, 12756, 6792, 142984, 120536, 51118, 49528, 2376],
index=['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto'])
planets = pd.DataFrame({'mass': mass, 'diameter':diameter})
planets.describe()
planets.describe
!pip install nbconvert
%shell jupyter nbconvert --to html /content/testfile.ipynb