Clean data
Get your data ready for analysis.
Last updated
Was this helpful?
Was this helpful?
# Assuming DataFrame df, pick the columns you want to drop
columns_to_drop = ['Average viewers', 'Followers']
df.drop(columns_to_drop, inplace=True, axis=1)# Replace row 7 in column 'Duration' with the value of 45
df.loc[7, 'Duration'] = 45# Specify things to replace empty strings to prep drop
df['col1'].replace(things_to_replace, what_to_replace_with, inplace=True)# Knowing your row, you can directly drop via
df.drop(x)
# Select a specific index, then drop that index
x = df[((df.Name == 'bob') &( df.Age == 25) & (df.Grade == 'A'))].index
df.drop(x)# Replace empty strings to prep drop
df['col1'].replace('', np.nan, inplace=True)
# Delete where specific columns are empty
df.dropna(subset=['Tenant'], inplace=True)# Specify column(s) to change data type
df.astype({'col1': 'int', 'col2': 'float'}).dtypes
# Common types: float, int, datetime, string# Drop duplicates across DataFrame
df.drop_duplicates()
# Drop duplicates on specific columns
df.drop_duplicates(subset=['col1'])
# Drop duplicates; keep the last
df.drop_duplicates(subset=['col1', 'col2'], keep='last')