Text: Python
How not clear?
Question 1
Use the code to simulate data and answer the questions below.
In [ ]:
import pandas as pd
import numpy as np
np.random.seed(1234)
s1 = pd.Series(np.random.randint(1, high=5, size=10, dtype='l'))
s2 = pd.Series(np.random.randint(1, high=4, size=10, dtype='l'))
s3 = pd.Series(np.random.randint(1e1, high=3e1, size=1e1, dtype='1'))
s4 = pd.Series(np.random.randint(1e3, high=3e4, size=1e1, dtype='1'))
1. (1) Create a DataFrame df by concatenating the three Series s1, s2, s3 as three columns (df: 100 by 3), and then rename column names to be 'a', 'b', 'c'. Keep the first 70 rows of df (df2: 70 by 3). Create another DataFrame df2 by joining the two Series s1, s4 by column (df2: 100 by 2), and the column names should be 'A' and 'B'. Then, keep the last 50 rows of df2 (df2: 50 by 2).
In [ ]:
# Your answer here
# for grading
print(df.columns,'\n')
print(df2.columns,'\n')
print(df.index,'\n')
print(df2.index,'\n')
1. (2) Merge df and df2 by the index values into df3. Note that we will only keep the intersections of the two dataframes (df3: 20 by 5).
In [ ]:
# Your answer here
print("part 2:\n")
# for grading
df3.head()
1. (3) Create a one column DataFrame with the values of the three Series s1, s2, s3 and assign it to df4 (300 by 1).
In [ ]:
# Your answer here
# for grading
print(type(df4)) #check the type of df4
print(df4.shape) #check the shape
print(df4.tail()) #check last columns: note the index
1. (4) Reindex the DataFrame so that the index is from 0 to 299.
In [ ]:
# Your answer here
# for grading
df4