This document collects pandas notes from:
031.pandas day 2.ipynb032.pandas class 3.ipynb033.pandas 4.ipynb034.pandas class homework discussed.ipynb035.pandas and numpy.ipynbtopic-wise/5. Pandas Class 23-25/9. iNeuron Pandas 23 - 25 Class.ipynb
pandas is a Python library for data analysis, providing Series and DataFrame as primary data structures.
import pandas as pddf = pd.read_csv('./data/sample-csv/titanic.csv')header=None→ no header rownames=[...]→ custom column namessep='|'orsep='\t'→ specify delimiterskiprows=[1, 3]→ skip specific rows
df = pd.read_csv('https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv')df = pd.read_excel('./data/sample-excel/LUSID Excel - Manage Orders.xlsx')xls = pd.ExcelFile('./data/sample-excel/LUSID Excel - Manage Orders.xlsx')
print(xls.sheet_names)
for sheet in xls.sheet_names:
sheet_df = pd.read_excel('./data/sample-excel/LUSID Excel - Manage Orders.xlsx', sheet_name=sheet)import requests
import re
url = 'https://www.basketball-reference.com/leagues/NBA_2015_totals.html'
headers = {'User-Agent': 'Mozilla/5.0'}
res = requests.get(url, headers=headers)
html = re.sub('<!--|-->', '', res.text)
df_list = pd.read_html(html)
df = df_list[0]df = pd.read_json('https://api.github.com/repos/pandas-dev/pandas/issues')df.head(5)
df.tail(5)
df.columns
df.dtypes
df.info()
df.describe()a = df['name'] # Series
sub_df = df[['name', 'location_id', 'id']] # DataFramedf.loc[0:4, ['order_id','order_date','ship_date']]
df.iloc[1:6, 3:6]df.isnull().sum()
missing_rows = df[df['Age'].isnull()]
df.fillna(value='sudh')
df.fillna(value=df['Age'].mean())df.dropna(axis=1)
df.dropna(axis=0, how='all')
df.dropna(thresh=3)df['ineuron'] = 'sudh'
df['New'] = df['SibSp'] + df['Parch']
df['sales'] = df['sales'].str.replace(',', '').astype(int)df['cabin_Number'] = df['Cabin'].str.replace('([A-Za-z]+)', '')
df['cabin_Letter'] = df['Cabin'].str.extract('([A-Za-z]+)')df[df['Age'] < 25]
df[(df['Survived'] == 0) & (df['Age'] < 40)]
df[df['Name'].str.startswith('S')]df.groupby('Pclass')['Pclass'].count()
df.groupby('Survived').describe()['PassengerId']
df1.groupby(['contact', 'y']).count()Examples:
- number of male/female passengers
- survived vs casualties
- count by ticket class
- average rating and weekly summaries
df2 = pd.concat([df2, df3])
df2 = pd.concat([df2, df3], axis=1)df_left = pd.merge(df4, df5, how='left', on='emp_id')
df_right = pd.merge(df4, df5, how='right', on='emp_id')pd.merge(df6, df7, left_on='emp_id1', right_on='emp_id2', how='inner')df2 = df2.set_index('age')
df3 = df3.set_index('age')
df2.join(df3, how='inner')data = {
'name': ['sudh', 'krish', 'hitesh', 'tulesko'],
'salary': [100, 200, 300, 400],
'mail_id': ['sudh@ineuron.ai', 'krish@ineuron.ai', 'hitesh@ineuron.ai', 'tulesko@ineuron.ai'],
'addr': ['blr', 'blr', 'blr', 'mum']
}
df = pd.DataFrame(data)df.to_csv('NBA.csv', index=False)pd.read_html()is useful for scraping HTML tables.- Use raw strings for Windows file paths:
r'.\data\sample-csv\taxonomy.csv'. pd.ExcelFile(...).sheet_nameslists worksheet names.pd.read_csv()supports remote URLs directly.- Use
.strfunctions for string extraction and replacement.
- Count male vs female passengers
- Count survivors and casualties
- Find the oldest passenger name
- Count passengers by class
- Count names starting with a specific letter
- Create a new column from existing columns
- Filter on age and survival conditions
- Separate text and numeric parts from a cabin field
- Analyze bank dataset with age, housing, loan, and campaign metrics
- Group by education or contact method and count
Generated from the selected pandas notebooks listed above.