<jupyter_start><jupyter_text>03 - Deleting & handling missing values - Wine  Step 1. Import the necessary libraries<jupyter_code>import pandas as pd
import numpy as np<jupyter_output><empty_output><jupyter_text>Step 2. Import the dataset from this [address](https://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data).   Step 3. Assign it to a variable called wine<jupyter_code>wine = pd.read_csv('wine.data')
wine<jupyter_output><empty_output><jupyter_text>Step 4. Delete the first, fourth, seventh, nineth, eleventh, thirteenth and fourteenth columns<jupyter_code>wine.drop(wine.columns[[0, 3, 6, 8, 10, 12,13]], axis=1, inplace=True)
wine<jupyter_output><empty_output><jupyter_text>Step 5. Assign the columns as below:The attributes are (donated by <NAME>, riclea '@' anchem.unige.it):  1) alcohol  2) malic_acid  3) alcalinity_of_ash  4) magnesium  5) flavanoids  6) proanthocyanins  7) hue<jupyter_code>wine.columns =['alcohol', 'malic_acid', 'alcalinity_of_ash', 'magnesium' ,'flavanoids', 'proanthocyanins', 'hue']
wine<jupyter_output><empty_output><jupyter_text>Step 6. Set the values of the first 3 rows from alcohol as NaN<jupyter_code>wine.iloc[0:3,:] = None
wine<jupyter_output><empty_output><jupyter_text>Step 7. Now set the value of the rows 3 and 4 of magnesium as NaN<jupyter_code>wine.loc[['3','4'],['magnesium']] = None
wine
wine.iloc[3:5].loc['magnesium'] = None
wine
wine.iloc[3:5,3] = None
wine<jupyter_output><empty_output><jupyter_text>Step 8. Fill the value of NaN with the number 10 in alcohol and 100 in magnesium<jupyter_code>wine['alcohol'] = wine['alcohol'].fillna(10)
wine
wine['magnesium'] = wine['magnesium'].fillna(100)
wine
wine[['malic_acid', 'flavanoids']] = wine[['malic_acid', 'flavanoids']].fillna(77)
wine<jupyter_output><empty_output><jupyter_text>Step 9. Count the number of missing values<jupyter_code>wine.isna().sum().sum()<jupyter_output><empty_output><jupyter_text>Step 10.  Create an array of 10 random numbers up until 10<jupyter_code>elarray = np.random.randint(1, 10, 10)
elarray<jupyter_output><empty_output><jupyter_text>Step 11.  Use random numbers you generated as an index and assign NaN value to each of cell.<jupyter_code>dfrandom = pd.DataFrame(None, index=elarray, columns=['A'])
dfrandom<jupyter_output><empty_output><jupyter_text>Step 12.  How many missing values do we have?<jupyter_code>dfrandom.isna().sum().sum()<jupyter_output><empty_output><jupyter_text>Step 13. Delete the rows that contain missing values<jupyter_code>wine = wine.dropna()
wine<jupyter_output><empty_output><jupyter_text>Step 14. Print only the non-null values in alcohol<jupyter_code>alc = pd.notnull(wine["alcohol"])
wine[alc]<jupyter_output><empty_output><jupyter_text>Step 15.  Reset the index, so it starts with 0 again<jupyter_code>wine.reset_index
wine<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup