Pandas Flashcards
Drop Columns
df.drop(columns=[‘Column1’, ‘Column2’])
Pivots
df.pivot(columns=’var’, values=’val’)
Sort
df.sort_values(‘column1’)
Order rows by values of a column (low to high).
Rename Columns
df.rename(columns = {‘y’:’year’})
Rename the columns of a DataFrame
Head
df.head(n)
Select first n rows
Tail
df.tail(n)
Select last n rows
Using Query
query() allows Boolean expressions for filtering rows. df.query('Length > 7') df.query('Length > 7 and Width < 8') df.query('Name.str.startswith("abc")', engine="python")
Select rows 10-20.
df.iloc[10:20]
Select columns in positions 1, 2 and 5 (first
column is 0).
df.iloc[:, [1, 2, 5]]
Access single value by index
df.iat[1, 2]
Access single value by label
df.at[4, ‘A’]
Select rows meeting logical condition, and only the specific columns .
df.loc[df[‘a’] > 10, [‘a’, ‘c’]]
Append rows of DataFrames
pd.concat([df1,df2])
Append columns of DataFrames
pd.concat([df1,df2], axis=1)
Gather columns into rows.
pd.melt(df)
Logic in Python (and pandas)
< Less than != Not equal to > Greater than df.column.isin(values) Group membership == Equals pd.isnull(obj) Is NaN <= Less than or equals pd.notnull(obj) Is not NaN >= Greater than or equals &,|,~,^,df.any(),df.all() Logical and, or, not, xor, any, all