What is the output of the following code that combines a NumPy array and a Pandas Series?
import numpy as np import pandas as pd arr = np.array([1, 2, 3]) ser = pd.Series([4, 5, 6]) result = arr + ser print(result)
NumPy arrays and Pandas Series can perform element-wise operations if their lengths match.
NumPy arrays and Pandas Series support element-wise addition when their lengths are the same. The addition adds corresponding elements.
What is the shape and type of the resulting object after converting a 2D NumPy array to a Pandas DataFrame?
import numpy as np import pandas as pd arr = np.array([[1, 2], [3, 4], [5, 6]]) df = pd.DataFrame(arr) print(df.shape, type(df))
Converting a 2D NumPy array to a DataFrame keeps the shape but changes the type.
The DataFrame has the same shape as the original array but is a Pandas DataFrame object.
Consider this code snippet:
import numpy as np import pandas as pd arr = np.array([1, 2, 3]) ser = pd.Series([4, 5]) result = arr + ser print(result)
Why does this code raise an error?
import numpy as np import pandas as pd arr = np.array([1, 2, 3]) ser = pd.Series([4, 5]) result = arr + ser print(result)
Check if the lengths of the arrays match for element-wise addition.
The NumPy array has length 3, but the Pandas Series has length 2. Their shapes do not align, causing a ValueError during addition.
What does the following code plot?
import numpy as np import pandas as pd import matplotlib.pyplot as plt arr = np.array([10, 20, 30, 40]) df = pd.DataFrame({'values': arr}) df.plot(kind='bar') plt.show()
DataFrame.plot(kind='bar') creates a bar chart of the column values.
The DataFrame has one column with 4 values. Plotting with kind='bar' creates a bar chart with 4 bars representing those values.
Which of the following best explains why interoperability between libraries like NumPy and Pandas matters in data science?
Think about how using multiple tools together helps in real projects.
Interoperability lets different libraries work together smoothly, so you can use the best features of each without extra work.