General Python Coding questions Flashcards
How do you determine if a number is divisible by three
num % 3 == 0
Round a number up
math.ceil(n)
How do you square a number
5**2
how to run a math equation?
eval(“1+3==4”)
how to replace a string character with a different character
“xyz”.replace(‘z’,’q’)
result: “xyq”
How to extract a list value into another list
[*[1,2,3,4],5]
How to write a sting in lower case
‘ABC’.lower()
What does sub() do
finds all substrings where the regex pattern matches and then replace them with a different string
what does subn() do
similar to sub but retuns the new sting along with the number of replacements
How to remove values from a python array
pop() and remove()
pop return the value removed
remove does not return a value
How to import modules in python
import array #importing using the original module name
import array as arr # importing using an alias name
from array import * #imports everything present in the array module
What is polymorphism in python
Polymorphism means the ability to take multiple forms. for instance if the parent class has a method named ABC then the child class also can have a method with the same name ABC having its own parameters and variables. Python allows this
What is used for floor division
//
What is the maximum possible length of an identifier
Identifiers can be of any length
how to tell if a file or directory exists?
os.path.exists(‘path’)
How to open a file
with open(‘file path’, ‘r’) as f:
—x = f.read()
With automatically closes the file
how to write a file
with open(‘file path’, ‘w’) as f:
—-f.write(‘text string here’)
this will create a new file if it doesn’t exist and over write that file if it already exists.
How to turn a pandas column into a list
df[“column”].tolist()
how to change pandas column type
df[‘column’].astype(‘str’)
In a pandas loc statement what symbol reverses true?
~
List comprehension
[x for x in [values] if x > 3]
Finding quick general stats about a pandas dataframe
df.describe()
Find the dataframes datatypes
df.dtypes
List number of rows and columns in a pandas dataframe
df.shape
list columns in a pandas dataframe
df.columns
list general information about a pandas dataframe
df.info()
how to get ride of duplicate rows
df.drop_duplicates()
Finding the total number of null values in a dataframe
loadmatch.isna().sum()
loadmatch.isna().any()
getting rid of a panda series rows
df.series.dropna()
methods for filtering data that is greater than or less than a specific number.
.le()
.ge()
ex: df.loc[df.column.ge(2000)]
filter dataframe where column contains certain values
.isin()
df.loc[df.column.isin([‘van’,’Reefer’)]
symbols for AND and OR in pandas loc
&, |
Pandas, Count the frequency a value appears in a column
.value_count()
ex: df[‘column’].value_count()
How to group and aggreagate data
groupby() and aggregate()
df.groupby([“Mode”, “MonthName”]).agg({“Rate”:”sum”})
Pandas Example of creating a new column
df.loc[loadmatch.Rate.le(100), “RateCategory”] = “Low”
This will add low to where the filter is set.
Pandas how to inner join columns by their indexes
.join()
ex: df.join(df2, how=”inner”)
—-these will join based on the index set.
Pandas how to combine data frames by specifying the columns
pd.merge()
ex: pd.merge(dataset1, dataset2, on=”Loadnum”, how=”inner)
ex: pd.merge(dataset1, dataset2,
left_on=[“loadnum”, “ActivityDate”, right_on=[“Loadnum”,
“ActivityDate”], how=”left”)