0
0
Pandasdata~10 mins

Creating DataFrame from NumPy array in Pandas - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a DataFrame from a NumPy array named 'data'.

Pandas
import pandas as pd
import numpy as np
data = np.array([[1, 2], [3, 4]])
df = pd.[1](data)
print(df)
Drag options to blanks, or click blank then click option'
ASeries
Barray
CDataFrame
Dconcat
Attempts:
3 left
💡 Hint
Common Mistakes
Using pd.array instead of pd.DataFrame
Using pd.Series which creates a one-dimensional structure
Trying to use pd.concat which combines multiple DataFrames
2fill in blank
medium

Complete the code to assign column names 'A' and 'B' when creating the DataFrame.

Pandas
import pandas as pd
import numpy as np
data = np.array([[5, 6], [7, 8]])
df = pd.DataFrame(data, columns=[1])
print(df)
Drag options to blanks, or click blank then click option'
A['A', 'B']
B('A', 'B')
C{'A', 'B'}
DA, B
Attempts:
3 left
💡 Hint
Common Mistakes
Using a tuple instead of a list for columns
Passing a set which is unordered
Passing column names without quotes
3fill in blank
hard

Fix the error in the code to correctly create a DataFrame from a NumPy array 'arr'.

Pandas
import pandas as pd
import numpy as np
arr = np.array([[9, 10], [11, 12]])
df = pd.DataFrame(arr, columns=[1])
print(df)
Drag options to blanks, or click blank then click option'
A['9', '10']
B['0', '1']
C['a', 'b', 'c']
D['X', 'Y']
Attempts:
3 left
💡 Hint
Common Mistakes
Providing more column names than columns in the array
Using numbers as strings that don't match columns
Using a list with wrong length
4fill in blank
hard

Fill both blanks to create a DataFrame from 'matrix' with index labels 'row1', 'row2'.

Pandas
import pandas as pd
import numpy as np
matrix = np.array([[13, 14], [15, 16]])
df = pd.DataFrame(matrix, columns=[1], index=[2])
print(df)
Drag options to blanks, or click blank then click option'
A['Col1', 'Col2']
B['row1', 'row2']
C['row1', 'row2', 'row3']
D['Column1', 'Column2']
Attempts:
3 left
💡 Hint
Common Mistakes
Using index list with wrong length
Using column names that don't match array shape
Swapping columns and index lists
5fill in blank
hard

Fill all three blanks to create a DataFrame from 'arr' with columns 'X', 'Y' and index 'a', 'b'.

Pandas
import pandas as pd
import numpy as np
arr = np.array([[17, 18], [19, 20]])
df = pd.DataFrame([1], columns=[2], index=[3])
print(df)
Drag options to blanks, or click blank then click option'
Aarr
B['X', 'Y']
C['a', 'b']
D['row1', 'row2']
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable name for data
Mismatching columns or index length
Using wrong data types for columns or index