Practical 3
Practical 3
CLEANING DATA
1 clean empty cell
import pandas as pd
df = pd.read_csv('data.csv')
new_df = df.dropna()
print(new_df.to_string())
#Notice in the result that some rows have been removed (row 18, 22 and 28).
import pandas as pd
df = pd.read_csv('data.csv')
df.dropna(inplace = True)
print(df.to_string())
#Notice in the result that some rows have been removed (row 18, 22 and 28).
#These rows had cells with empty values.
3 Convert to date:
import pandas as pd
df = pd.read_csv('dta.csv')
df['Date'] = pd.to_datetime(df['Date'])
print(df.to_string())
5 Replacing Values
import pandas as pd
df = pd.read_csv('data.csv')
df.loc[7,'Duration'] = 45
print(df.to_string())
Removing Duplicates
import pandas as pd
df = pd.read_csv('data.csv')
print(df.duplicated())
example
import pandas as pd
df = pd.read_csv('data.csv')
df.drop_duplicates(inplace = True)
print(df.to_string())