Pandas DataFrame Operations Practice Exam
Question 1 of 50
df.fillna(method='ffill')
df.ffill()
df['col'].map(func)
df['col'].apply(func)
map
df.sort_index()
ascending=False
df.loc[mask, 'col'] = new_value
SettingWithCopyWarning
df['col']
df.col
Series
df.drop('col', axis=1)
del df['col']
df.apply(np.sum)
axis=1
df['v'].rolling(window=3).mean()
window-1
df['col'] = df['col'].astype(int)
int
float
str
'category'
datetime64[ns]
df.describe()
q1 = df['v'].quantile(0.25); q3 = df['v'].quantile(0.75); iqr = q3 - q1; mask = (df['v'] < q1 - 1.5*iqr) | (df['v'] > q3 + 1.5*iqr)
df.mean()
df.groupby('key').agg(mean_a=('a','mean'), max_b=('b','max'))
df['col'].str.split(',', expand=True)
df['col'].str.extract(r'(\d+)')
df.isna().sum()
df.copy()
df.sort_values(['a', 'b'], ascending=[True, False])
pd.read_excel('file.xlsx', sheet_name='Sheet1')
inner
df['full'] = df['first'] + ' ' + df['last']
df['full'] = df[['first','last']].agg(' '.join, axis=1)
df.query('a > 0 and b < 10')
and
or
df.
df['col'].str.replace('old', 'new', regex=False)
regex=True
df.replace({'old1': 'new1', 'old2': 'new2'})
df.count()
df.drop('col', axis=1, inplace=True)
df
df = df.drop('col', axis=1)
pd.cut(df['v'], bins=[0,10,20,100])
pd.qcut(df['v'], q=4)
for row in df.itertuples(): ...
iterrows
pd.crosstab(df['a'], df['b'])
values=
aggfunc=
value_counts()
df[df['col'].isna()]
notna()
df['c'] = df['c'].astype('category')
df.index
df.iat[2, 3]
for name, group in df.groupby('key'): ...
df.dropna()
how='all'
(df['v'] - df['v'].mean()) / df['v'].std()
df.iloc[row_positions, col_positions]
df['new'] = df['a'] + df['b']
merge
join
df['date'].dt.year
.dt.month
.dt.day
.dt.dayofweek
.dt.hour
df.sort_values('col')
df.sort_values('col', ascending=False)
df1.join(df2)
pd.concat([df1, df2], axis=1)
df.drop_duplicates()
subset=['col1','col2']
df['v'].expanding().mean()
df[['col1', 'col2']]
df[df['a']>0]['b'] = 1
df.loc[df['a']>0, 'b'] = 1
df.fillna(0)
df.fillna({'a': 0, 'b': -1})
pd.merge(df1, df2, left_on='id1', right_on='id2')
isin
df[df['col'].isin(['a','b','c'])]
df.cov(numeric_only=True)
df.idxmax()
df['col'].idxmax()
df.mean(axis=1)
df.resample('M').mean()
'D'
'W'
'M'
'Q'
'Y'
'H'
df.replace([np.inf, -np.inf], np.nan)
df['col'].str.startswith('pre')
endswith
contains
df['col'].argmax()
df['col'].to_numpy().argmax()
df.rename(columns={'old': 'new'}, inplace=True)
df.rename(columns=str.lower)
df['col'].nunique()
df['col'].value_counts()
df.isna()
pd.merge(df1, df2, left_index=True, right_index=True)
df.to_csv('file.csv', index=False)
index=False
df.groupby('key').mean(numeric_only=True)
filter()
groupby
df.groupby('key').filter(lambda g: g['v'].mean() > 0)
df.sample(n=5)
df.sample(frac=0.1, random_state=42)
df[df['col'] == value]
df.loc[df['col'] == value]
df['v'].clip(lower=0, upper=100)
iloc
import pandas as pd
pd.DataFrame([{'a': 1}, {'a': 2, 'b': 3}])
NaN
df.duplicated()
df.duplicated().sum()
df.groupby('key').agg({'a': 'mean', 'b': ['min','max']})
df.groupby(['k1','k2']).sum()
df.pivot(index='id', columns='var', values='val')
df['v'].pct_change()
(v_t - v_{t-1}) / v_{t-1}
pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
pd.read_csv('file.csv')
|
df[(df['a']>10) | (df['b']<0)]
df.dropna(axis=1)
df['c'] = pd.Categorical(df['c'], categories=['low','med','high'], ordered=True)
df.corr(numeric_only=True)
df['v'].shift(1)
shift(-1)
transform()
for index, row in df.iterrows(): ...
df.groupby('key')['v'].transform('mean')
pd.to_datetime(df['date_str'])
format='%Y-%m-%d'
df.iloc[0]
df['col'].str.strip()
lstrip()
rstrip()
unstack
df.columns
Index
stack
df1.equals(df2)
df1 == df2
pd.merge
df.map()
df.loc[row_labels, col_labels]
df.iloc[5:10]
pd.read_json('file.json')
pd.read_json('file.json', orient='records')
pd.merge(df1, df2, on='key')
how='left'/'right'/'inner'/'outer'
df.dtypes
Question navigator