1/29
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
set( )
{ }
a collection that stores unique values (no duplicates) and is unordered
use .add( ) and .remove( )
range(3)
0, 1, 2
4%3
1 remainder
15 // 4
there are 3 4s in 15
HashSet
a data structure that stores unique elements in no particular order. No duplicate item, cannot access elements by an index, allow null
// for Sudoku
// = "which group?" (top/middle/bottom)
row = (nsquare//n)*x+i # how manyn*n box,
x num box per row,
nsquare is 第几个box
% for sudoku
% = "which position inside the group?" (left/middle/right)
col = (nsquare% n)* x +j # remaindern*n box,
x num box per row,
nsquare is 第几个box
s[::-1]
s[::-1] means:
start : end : step
-1 = go backwards
\
line continuation character. It means this statement continues on the next line.
world[name] what type does this output
Returns one column as a Series.
world[['name']] what type does this output
dataframe
~
not
filter those name starting with A
df[df['name'].str.startswith('A')]filter name containing John
df[df['name'].str.contains('John')]df.sort_values('column_name')Ascending (default) 少到多
renamedf.rename(columns={'old_name': 'new_name'})unique
drop_duplicates()df['name'].str[0:3]
substring
string spaces
.str.strip()
df['name'].str.split(' ')
split by space
df['name'].str.len() how to use this
df[df['name'].str.len() > 5]
replace certain string
.str.replace(old, new)
lambda
means a small function you write in one line.
lambda arguments: expression
def bonus(x):
return x['salary']
becomes lambda x: x['salary']
what does axis 1 mean in apply
axis=1 mean apply function to each row
how to use assign ? what does it do ?
df.assign(new_column=value)
create or modify columns in a DataFrame.
how to use apply ?
df.apply(function, axis=1)
def get_bonus(x): return x['salary'] * 2 df['bonus'] = df.apply(get_bonus, axis=1)
df['bonus'] = df.apply(lambda x: x['salary'] * 2, axis=1).assign() syntax
df.assign(new_column=value)df.assign( bonus=df['salary'] * 0.1 )
what does assign do
.assign() lets you create or modify columns and returns a new DataFrame.
df.assign(new_column=value)change data
astype(int)fill in the missing value as 0 in the quantity column
.fillna(0, inplace=True)