Question: #please answer in python codeblock Use the python code below to generate your two pandas dataframes. import pandas as pd cells = {A: {1: A1,
#please answer in python codeblock
Use the python code below to generate your two pandas dataframes.
import pandas as pd
cells = {"A": {1: "A1", 2: "A1", 3: "A3"}, "B": {1: "B1", 2: "B2", 3: "B2"}, "C": {1: "C1", 2: "C2", 3: "C3"}, "D": {1: "1", 2: "2", 3: "3"}} df = pd.DataFrame(cells)
cells = {"A": {1: 1, 2: 5, 3: 3}, "B": {1: 2, 2: 6, 3: 3}, "C": {1: 3, 2: 7, 3: 3}, "D": {1: 4, 2: 8, 3: 3}} dfnumbers = pd.DataFrame(cells)
Now, answer the questions so you create the output listed below the question. Your dataframe (printed using print(df) or print(dfnumbers)) should look like the dataframe shown below the question.
Question 5: create a new column in dfnumbers called 'mult' and make it the product of df['D'] and dfnumbers['A'], then print dfnumbers A B C D mult 1 1 2 3 4 5.0 2 5 6 7 8 50.0 3 3 3 3 3 45.0
Question 6: append dfnumbers to the end of df. Df should now be 6 rows. Print df A B C D mult 1 A1 B1 C1 5.0 NaN 2 A1 B2 C2 10.0 NaN 3 A3 B2 C3 15.0 NaN 1 1 2 3 4.0 5.0 2 5 6 7 8.0 50.0 3 3 3 3 3.0 45.0
Question 7: Fill the NaN values in df with 99 A B C D mult 1 A1 B1 C1 5.0 99.0 2 A1 B2 C2 10.0 99.0 3 A3 B2 C3 15.0 99.0 1 1 2 3 4.0 5.0 2 5 6 7 8.0 50.0 3 3 3 3 3.0 45.0
Question 8: in dataframe df, set column 'B' to be the index and drop column B A C D mult B B1 A1 C1 5.0 99.0 B2 A1 C2 10.0 99.0 B2 A3 C3 15.0 99.0 2 1 3 4.0 5.0 6 5 7 8.0 50.0 3 3 3 3.0 45.0
Question 9: change the names of the columns of df to 'first','second','third','fourth' first second third fourth B B1 A1 C1 5.0 99.0 B2 A1 C2 10.0 99.0 B2 A3 C3 15.0 99.0 2 1 3 4.0 5.0 6 5 7 8.0 50.0 3 3 3 3.0 45.0 Question 10: using the pivot command, pivot dataframe df with index being the first column, the columns being the second column, and the values being the fourth column. There will be NaN's second 3 7 C1 C2 C3 first 1 5.0 NaN NaN NaN NaN 3 45.0 NaN NaN NaN NaN 5 NaN 50.0 NaN NaN NaN A1 NaN NaN 99.0 99.0 NaN A3 NaN NaN NaN NaN 99.0
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
