Python Flashcards
(187 cards)
Dataframes: How to select all rows of a df where in col1 “Black” and “White” appear?
df[df[“col1”].isin([“Black”,”White”])]
Dataframes: How to select 2 columns?
df[[“col1”,”col2”]]
Dataframes: How to sort 2 columns of a df in descending order?
df.sort_values([“col1”,”col2”], ascending=[False, False])
Dataframes: How to get the values of a df?
df.values
Dataframes: How to get the description of a df?
df.describe()
Dataframes: How to get a shape of a df?
df.shape
Dataframes: How to get info about a df?
df.info()
Merging to the nearest date-times: How to replicate VLOOKUP(range_lookup=TRUE)?
pd.merge_asof(df1,df2,direction=”backward”) - direction:backward, forward, nearest
Advantage of pd.merge_ordered() vs. pd.merge?
pd.merge_ordered() has fill_method
Joining data: 2dfs (current, drafted) merged on column name and having the suffixes (‘_current’, ‘_drafted’) - how to sort the new df based on the column on which was merged?
current.merge(drafted, on=”name”, suffixes=(“_current”, “_drafted”), sort=True)
Joining data: Merge 2dfs (current, drafted) with an outer join and suffixes (‘_current’,’_drafted’) - how to show from which df a value is coming from?
current.merge(drafted, how=”outer”, on=”name”, suffixes=(“_current”,”_drafted”), indicator=True)
Joining data: Merging the df teams having the column player_id with df positions having player_id as index?
teams.merge(positions, left_on=”player_id”, right_index=True)
What are the joint default values for pd.merge and pd.merge_ordered?
pd.merge=inner join, pd.merge_ordered=outer join
Dataframes: How to use pd.merge_ordered with “ffill”?
pd.merge_ordered(df1, df2, on=”Date”, fill_method=”ffill”)
Dataframes: How to merge two dataframes with outer and sort index column called “Date”?
pd.merge(df1, df2, how=”outer”).sorted_values(“Date”)
Dataframes: How to join dataframes with integrated .join function with an inner join?
df1.join(df2, how=”inner”)
Dataframes: How to make an “inner” join with pd.merge on 2 columns and 2 suffixes?
pd.merge(df1, df2, on=[“col1”,”col2”], suffixes=[“_1”,”_2”], how=”inner”)
Dataframes: How to merge two dataframes on different columns in both dataframes?
pd.merge(df1,df2, left_on=”col1”, right_on=”col2”)
Dataframes: How to merge on several columns and/with using suffixes?
pd.merge(df1,df2,on=[“col1”,”col2”],suffixes=[“_1”,”_2”])
Dataframes: How to merge on several columns?
pd.merge(df1,df2,on=[“col1”,”col2”])
Dataframes: How to merge on one specific column?
pd.merge(df1,df2,on=”col1”)
Dataframes: What happens if pd.merge() is applied on 2 dataframes without any additional arguments?
computes a merge on all columns that occur in both dataframes, this is by default an “inner” join because it glues together only rows that match in the joining column of BOTH dataframes
Dataframes: Pandas: How to extend/ replace concat() with the ability to align rows using multiple columns?
pd.merge()
Dataframes: how to sort data by index?
df.sort_index()