Analysing data with pandas: descriptive statistics, sorting, groupby, missing values with count, isnull, dropna and fillna, and moving data between pandas and MySQL.
| mean() | total ÷ number of values present: (74 + 25 + 21 + 60 + 45) ÷ 5 = 45.0 |
| dropna().shape | (rows with no missing value, the same number of columns) |
| fillna(0).sum() | the same total as sum(), printed as a float |
| groupby | df.groupby('Store')['Price'].max(); adding ['North'] picks one group |
| Range of a column | df['Marks'].max() - df['Marks'].min() |
| describe() | count, mean, std, min, quartiles and max of each numeric column |
DataFrame df:
index | Item | Store | Price | Stock
P1 | Eraser | South | — | 90
P2 | Diary | North | 74 | 50
P3 | Pen | East | 25 | 27
P4 | Notebook | East | 21 | 56
P5 | Folder | East | 60 | 72
P6 | Crayons | North | 45 | 75
(— marks a missing value; pandas prints it as Not a Number.)
Write the output of:
(a) print(df['Price'].count())
(b) print(df['Price'].sum())
(c) print(df.dropna().shape)
Working: (a) and (b) skip missing values, and the column is a float column; (c) drops the rows with a missing value
Answer: (a) 5; (b) 225.0; (c) (5, 4)