''' Working with Pandas data frame Pandas stores data in a 2D table called a DataFrame. In this exercise we will learn how to work with such a data frame. ''' ''' Exercise A: Creating a DataFrame For our exercise purposes, we will create a simple DataFramee. To do that, you first create a dictionary, with the column title corresponding to the data for that column. For example, to get a column of ages and names, the command would be: data_friends = {"Name": ["Alex","Alin", "Lucia", "Tessa"], "Age": [22,21,23,21]} We can convert this into a dataframe using: df_friends = pd.DataFrame(data). Similarly, create a DataFrame with the names, ages, gender and hair colour of your friends and/or family members. ''' # Your code here ''' Exercise B: Creating a DataFrame II Another way to create a DataFrame is directly via the `pd.DataFrame` command. For this, you must specify the data, column names and index labels (if present). The index labels will replace 0,1,2,3 etc., as the labels for the rows. For example, to create the friends DataFrame we can write: df_friends = pd.DataFrame([[22,"male","brown"], [21,"male","brown"], [23,"other","brown"], [21,"female","brown"]], columns= ["Age","Gender","hair colour"], index= ["Alex","Alin","Lucia","Tessa"]) Note that in this case, we don't need the column label "names" as the names are now not a column, but the labels for the rows. Create a DataFrame with the same data as above directly using the DataFrame command. For the exercises below, we will be using this DataFrame. ''' # Your code here ''' Exercise C: Retrieving specific data from DataFrames In DataFrames, it's possible to access only certain rows or columns of a data, just as with matrices. To get a specific column, use `df_friends["gender"]` or any other column name. To access a row, we use the command `df_friends.loc["Lucia"]` to access the row with the label "Lucia". From DataFrame `df_friends`: 1. Create and print series (a 1D array in pandas) of the ages of your friends. 2. Create and print a series that gives all the data of one of your friends. 3. We can also use the command `df_friends.iloc[2]` to get all the information about Lucia, where we use 2 to denote the row we want to access. Try it yourself! 4. We can also get only the rows that correspond to a specific condition. Give all rows with male as gender. ''' # Your code here ''' Exercise D: Iterating over rows We can iterate over rows using `dataframe_name.iterrows()`. Use the command: for i,j in df_friends.iterrows(): print(i,j) What do `i` and `j` represent in this case? What happens if you iterate only over one, not two variables, e.g., only `i`? Print the types for `i` and `j` in the first, and just `i` in the second case. ''' # Your code here ''' Exercise E: Iterating over columns To iterate over a column, we don't have a specific command. Rather, we first extract the column names by converting the dataframe into a list. Check for yourself that doing so will give you only the names of the columns! We can now iterate using column indexing (as seen earlier in this exercise) and a `for` loop over the column names. Create a `for` loop that prints the contents of each column `i`. ''' # Your code here ''' Exercise F: Mathematical operations We can perform mathematical operations on DataFrames and Series similar to how we work with matrices and arrays in NumPy. Take the DataFrame: df_abcd = pd.DataFrame([[2,6,3,6],[5,2,8,5],[3,1,7,5],[6,7,4,8]], columns= ["A","B","C","D"]) Part I. Perform the following operations: 1. Multiply by 4 2. Subtract 7 3. Multiply the dataframe by 2*pi and take the sine 4. Find the mean value of the columns of the DataFrame, and of the rows of the DataFrame Part II. We can also perform operations with two Series. Next take the 0th and 1st row of the DataFrame and multiply them. Then, subract the 0th row from the data frame. ''' # Your code here