Does pandas depend on NumPy?

PandasServer Side ProgrammingProgramming

Pandas is built on top of NumPy, which means the Python pandas package depends on the NumPy package and also pandas intended with many other 3rd party libraries. So we can say that Numpy is required for operating the Pandas.

The pandas library depends heavily on the Numpy array for the implementation of pandas data objects.

Example

import pandas as pd
df = pd.DataFrame({'A':[1,2,3,4], 'B':[5,6,7,8]})
print('Type of DataFrame: ',type(df))

print('Type of single Column A: ',type(df['A']))

print('Type of values in column A',type(df['A'].values))

print(df['A'].values)

Explanation

df variable stores a DataFrame object created by using python dictionary, this DataFrame having 2 columns named as A and B. In the third of the above code, we are trying to display the type of our dataFrame it will display pandas core Dataframe. The fourth line will print the type of single column which is A the resultant output will be pandas Series. The fifth line is going to display the type of values available in that single column A.

Output

Type of DataFrame: <class 'pandas.core.frame.DataFrame'>
Type of single Column A: <class 'pandas.core.series.Series'>
Type of values in column A <class 'numpy.ndarray'>

array([1, 2, 3, 4], dtype=int64)

The third line of the output displays that data is representing the Numpy array object in our above pandas example. In our example, we are not even imported the NumPy package.

Example

import pandas as pd
df = pd.DataFrame([['a','b'],['c','d'],['e','f'],['g','h']], columns=['col1','col2'])
print('Type of DataFrame: ',type(df))

print('Type of single Column A: ',type(df['col1']))

print('Type of values in column A',type(df['col1'].values))

print(df['col1'].values)

Explanation

In the following example, we have a DataFrame df created by using python lists of lists. This DataFrame df has 2 columns named col1 and col2. We try to print the type of single column “col1” and the resultant output will be pandas Series. If we print the type of values available in that column col1 we can see that the output will be numpy.ndarray.

Output

Type of DataFrame: <class 'pandas.core.frame.DataFrame'>
Type of single Column A: <class 'pandas.core.series.Series'>
Type of values in column A <class 'numpy.ndarray'>
['a' 'c' 'e' 'g']

Now we can say that the pandas columns can be built on the basics of the NumPy array object. You don’t need to import it specifically when working with Pandas. And when you install Pandas you can see that your package manager will automatically install the Numpy package if you have not installed NumPy before.

raja
Published on 17-Nov-2021 05:41:45
Advertisements