0% found this document useful (0 votes)
4 views

Python CheatSheet horizontal (2)

Uploaded by

madhu2401008
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python CheatSheet horizontal (2)

Uploaded by

madhu2401008
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Chapter 1: Variables: File creation (txt): Chapter 7 (SQL):

print ("The " + cake_name + "is $ " + cake_price) a = "Singapore Zoo" filename = "hello" 1) SELECT Username from users, DOB as Birthday FROM
inventory { "choco": 20, "vanilla": 15} print (a[:3]) → first 3 characters file = open(filename, "w") USERS;
print (inventory ["choco"]) print (a [-3:]) → last 3 characters row = var 1 + "," + var 2 2) SELECT Username from users, DOB as Birthday FROM
In print statements all arguements must be string (x print (a [5:9]) → prints "pore" file.write crow) # write row to file USERS WHERE DOB < “1990 - 01-01”;
float/ integer) file. close 3) SELECT content, Likes
ratings = [5, 4,3] a = a.replace [ "z", "B") → replaces z with B FROM Posts
sum_ratings = sum(ratings) a = q.replace ("o", "a") → replaces all Os with a File creation (csv): INNER JOIN Users ON Post. UserID = Users. UserID
(output = Singapore Zaa) import csv WHERE users. username = 'James Lee' ;
a = a.replace(" o", "a", 1) → replaces first occurrance of filename = "cute.csv" 4) SELECT Location, COUNT(*) as count
Chapter 2:
o to a with open (filename, "w", newline = " ") as fp: From Users
add + subtract - multiply * divide (float) / power ** (for
(output = singapare zoo) csv_w= csv.writer (fp) GROUP BY Location;
square root use **0.5 or math.sqrt()) divide (int) //
a = a + "bay" → adds bay at the end csv_w.writerow('var1', 'var 2') #creating titles 5) ON Users.UserID = Friende.userIDI
modulus/ remainder %
a = a[:10]+ “ bay ”+ a [10:] → adds bay in the middle row = [var1, var 2] WHERE Users. Username LIKE "James Lee";
if: else:
csv_w.writerow(row) 6) SELECT * from HR;
Try: except ValueError: List: 7) SELECT DISTINCT position FROM HR ORDER BY
list_a = [ 'a' 'b', 'c', 'e'] File reading: position;
Chapter 3: for i in range(1,11) list. a [3]= 'd' → replaces 'e' with 'd' data = [ ] 8) SELECT X,Y,Z,
i=0 print(i) list_a. insert ( 3, 'd') →adds 'd' before 'e' with open('cute.txt', 'r') as fp: A as B
while i<x [11 is last number + 1, 11-1 = number of list_a.remove('e') / list_a. pop (3) → removes'e' for line in fp: FROM HR
i+=1 times loop should run] line_data = line. strip(). Split (',') 9) SELECT COUNT(*) FROM HR WHERE sex = 'M' ;
print (i) Dictionary: line_data [3] = float (line data [3]) 10) SELECT MIN (salary) AS 'min pay',
a_dict = { 'city': 'sg', 'flower': 'orchid' } x=0 MAX(salary) AS 'max pay'
list.append(i) [list is name of list, i is name of variable] a_dict [ 'location'] = 'bay' → adds new key and value for i in data: AVG(salary) AS' Avg pay'
print (f" The mean number of the entered is: { mean }") a_dict [ 'flower'] = 'rose' → replaces value for an existing if i[3] >37.5: FROM HR;
mean = sum (list) / len (list) key x+=1 11) SELECT *
del a_dicy [ 'city'] / a_dict. pop ('city') → removes valve FROM HR
and key data= [ ] WHERE salary BETWEEN 20000 AND 30 000;
Chapter 4: Chapter 5: a_dict['country'] = a_dict. pop ( 'city') month = [xxx] 12) WHERE position = 'Supervisor' OR position = 'manager"; /
for char in word: attraction = "Singapore Zoo" * string is immutable Item = [xxx] WHERE position IN('supervisor', 'manager');
for char in v: print (attraction [10:14]) → for i in range (len (item)) : 13) WHERE NOT (Position = 'Supervisor' OR position =
output: "zoo" Chapter 6: row = [] manager'); / WHERE position NOT IN (' supervisor', 'manager');
list_a = [] from datetime import date % Y → Year for m in month: 14) WHERE name LIKE ‘% o%'; [ contains the letter 'o']
def satisfaction (n): list_a = [ "Zoo", "Garden"] to_day = date. today () % m → month sales = int (input (xxx)) WHERE name LIKE '% y’ [name ends with the letter y]
for i in range print (list_a [0]) → output: "Zoo" print (to_day) → 2024-11-29 % d → day row.append(sales) 15) SELECT salary WHERE salary > 1000 ORDER BY ASC
(1,n+1): print (today. year) → 2024 % H → hour data. append(row) 16) DELETE data WHERE Date = “2023-07-06”
try: sales_L= [xxx] print (to-day. month) → 11 % M → Minute print ("Sale for item 1 in March", data[1][2])
a = int (input sales_m =[xxx] print (today.day) → 29 % S → second
(xxx)) month = [xxx] Miscellaneous: writelines() → Writes a list of strings to the file. Each
list-a.append (a) c_dict = {} import datetime / from datetime import datetime readlines() → Reads all the lines from the file and string in the list is written as is; must include newline
c_dict['L'] = sales _L to_day = datetime.date.today() / to_day = returns them as a list of strings characters (\n) explicitly if you want line breaks
except ValueError: c_dict['m'] = sales_m datetime.datetime.today() deadline()→ Reads a single line from the file and write() → Writes a single string to the file. Use this for
print (xxxx) c_dict [ 'month'] = month x = datetime.delta(days =1) returns it as a string smaller, incremental writes
return list_a for i in range (len(sales _L)): [days can be weeks/hours/minutes/seconds and 1 read() → Reads the entire file content as a single string writerow() → Writes a single row to a CSV file. Each
print ("In { }, L get $ { } while m can be positive/negative] readrow() → Reads a single row from a CSV file element of the row is written to a separate column.
n = int(input (xxx)) gets $ {}." . format(c_dict [ print (“One day later: ”, to_day + x) readrows() → May refer to reading multiple rows at once writerows() → Writes multiple rows to a CSV file.
print(satisfaction (n)) 'months' ] [ each ], c_dict['L"] (not a standard method in Python) Each row is a list, and all rows are written at once
independence = datetime(year =1965, month =8, day
[each], c_dict['m'] [each])) Variable naming cannot be the same as preset defined
=9) dp: operators:
age = to_day - independence x = 3.14159 >,<,>=,<=,==,!= words eg True. Cannot start with a number but can end
from datetime import datetime, date, time, timedelta age_in_years = age.days/365.25 with one. Cannot contain special characters other than
y = 2.71828
moment datetime.now() Increment: +=,-=,*=,/= _. No white spaces
print(f"x: {x:.2f}, y: {y:.2f}")
print (moment) → output: 2024 - 11 - 29 15:23:45 % a → day of the week (short form)
Insert function not available on csv
print (moment. strftime ( "%d / %m / %Y")) % A → day of the week The input function can only take one string argument for its prompt,
The program will raise a IOError when
print (moment. strftime ("%H : % M")) % B → Month so it should not have a comma-separated second argument.
attempting to work on a closed file.
Understanding data:
Chapter 8: (Pandas) Chapter 9: Chapter 10: Pie Chart:
df. index
df. head Describing data: Scatterplot: import pandas as pd
df.values
df. shape → no. of rows and columns df. describe() import matplotlib.pyplot as plt import matplotlib.pyplot as plt
df.xxx | df ['xxx']
df. shape [1] → no. of columns df. corr() channel_1_data = df[df["Distribution
df. loc [ Row No]
df. columns.tolist () → column name df.quatile ([0.5,0.7, 0.8, df = pd.read_csv("job_market.csv") Channel"] == 1]
df. loc[ Row No, 'colname']
df.dtypes → type of data in each column 0.90]) plt.figure(figsize=(12, 6)) goods_columns = ['Cold Food', 'Dairy',
df.iloc[rowno.]
df [df-duplicated()] → shows duplicated records df.mean() plt.scatter(df['year'], df['recruitment_rate'], 'Grocery', 'Frozen']
df.iloc[row, col]
df-isna() →null records df.median() label='Recruitment Rate') distribution =
df [df.colname > xxx]
df isna().sum() → no. of null records df.mode () plt.title('Overall Recruitment Rate', channel_1_data[goods_columns].sum()
df-iloc[df.colname >xxx & xxxx]
df.describe → statistical summary df.std() fontsize=14) plt.pie(distribution, labels=distribution.index,
df.loc[:,:] → select all rows and
df-head (5) → first 5 records df. min() plt.xlabel('Year', fontsize=12) autopct='%1.1f%%', startangle=90,
columns
df. tail (2) → last 2 records df. max() plt.ylabel('Recruitment Rate', fontsize=12) colors=plt.cm.Paired.colors)
df[-10:] → select the last 10 rows
df. columms.str. strip()→ remove white space df sum() plt.show() plt.title("Distribution of Goods in Channel 1",
of the df
df.dropna() → remove null values df.groupby() fontsize=14)
df.drop(columns = ['x', 'y']) categories = df["categories"].unique() # plt.axis("equal")
df. xxx. astype (str) → convert to string df.iloc[144: 147,] (147 = last row plus 1) Get unique categories plt.show()
df. drop_duplicates (subset = ['xxx'], keep = 'last', df.iloc[2:5, 1:6] colors = ['blue', 'orange', 'green'] # Colors
inplace=True) df [[ 'xxx', 'xxx']] for the groups Bar Chart:
df['xxx']. unique() df.iloc[ : , [0,1,2]] for category, color in zip(categories, channel_1_data = df[df["Distribution
dft'xxx']. nunique () → count of unique values colors): Channel"] == 1]
df ['xxx']. notnull().sum() df.groupby ('room-type') [ 'price' ]. std (). max(). category_data = df[df["categories"] == goods_columns = ['Cold Food', 'Dairy',
df [df ['xxx'] == 'xxx']. count() df. groupby('Distribution Channel').sum().sum (axis=1). category] 'Grocery', 'Frozen']
df. replace ("xxx" , "xxx", inplace=True) sort _value (ascending = False) plt.scatter(category_data["year"], distribution =
df. rename (columns = { 'xxx'='xxx'}, inplace=True) df.groupby (df ['sales'] > 1000). min() category_data["recruitment_rate"], channel_1_data[goods_columns].sum()
df [(df ['xxx'>10) | (df ['xxx'] == 'xxx')] label=category, color=color) distribution.plot(kind='bar', color='skyblue')
plt.title("Total Distribution of Goods in
Dictionaries do not allow Line Chart: Channel 1", fontsize=14)
duplicate keys. Each key in a average_resignation = df.groupby("year plt.xlabel("Goods", fontsize=12)
dictionary must be unique. If ["resignation_rate"].mean().reset_index() plt.ylabel("Total Quantity", fontsize=12)
you try to add a duplicate key plt.plot(average_resignation["year"], plt.xticks(rotation=45, ha='right')
to a dictionary, the new value average_resignation["resignation_rate"], plt.show()
will overwrite the existing color='blue', label='Average Resignation
value associated with that key. Rate') Heatmap:
plt.title("Average Resignation Rates Over import pandas as pd
df['Department'].value_counts() the Years", fontsize=14) import seaborn as sns
→ used to count the number of plt.xlabel("Year", fontsize=12) import matplotlib.pyplot as plt
employees in each department plt.ylabel("Average Resignation Rate", correlation_matrix = df.corr()
in the df fontsize=12) sns.heatmap(correlation_matrix, annot=True,
plt.legend() cmap='coolwarm', fmt='.2f', linewidths=0.5)
plt.show() plt.title("Correlation Matrix Heatmap",
fontsize=14)
plt.show()

Seaborn: df.dropna(axis=1) → Drop all columns


Bar plots with any missing values from the df
Correlation heatmaps
Box plots Boxplots and violin plots df.drop_duplicates(keep = “last”) → Drop
Violin plots Pair plots (visualizing all duplicate rows and keep only the last
Strip plots relationships between occurrence in the df
Count plots multiple variables)
Distribution plots (e.g., df.dropna(subset=[’Salary’]) → Drop all
Regression plots (fitting rows where the "Salary" column has a
histograms, KDEs) regression lines to data) missing value

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy