Pandas and NumPy work together to help you handle and analyze data easily. Pandas uses NumPy arrays inside to store data efficiently.
0
0
Pandas and NumPy connection
Introduction
When you want to perform fast calculations on table data.
When you need to convert data between Pandas DataFrames and NumPy arrays.
When you want to use NumPy functions on Pandas data.
When you want to create Pandas objects from NumPy arrays.
When you want to access the raw data inside a Pandas DataFrame or Series.
Syntax
Pandas
import pandas as pd import numpy as np # Convert NumPy array to Pandas DataFrame pd.DataFrame(arr) # Convert Pandas DataFrame to NumPy array dataframe.values # Use NumPy functions on Pandas data np.mean(dataframe['column'])
Pandas DataFrames and Series store data as NumPy arrays internally.
You can easily switch between Pandas and NumPy formats for flexibility.
Examples
This creates a Pandas DataFrame from a NumPy array.
Pandas
import numpy as np import pandas as pd arr = np.array([[1, 2], [3, 4]]) df = pd.DataFrame(arr) print(df)
This extracts the NumPy array from a Pandas DataFrame.
Pandas
import pandas as pd df = pd.DataFrame({'A': [10, 20], 'B': [30, 40]}) arr = df.values print(arr)
This uses a NumPy function to calculate the mean of a DataFrame column.
Pandas
import numpy as np import pandas as pd df = pd.DataFrame({'A': [1, 2, 3]}) mean_val = np.mean(df['A']) print(mean_val)
Sample Program
This program shows how to convert between NumPy arrays and Pandas DataFrames and use NumPy functions on Pandas data.
Pandas
import numpy as np import pandas as pd # Create a NumPy array arr = np.array([[5, 10], [15, 20], [25, 30]]) # Convert NumPy array to Pandas DataFrame df = pd.DataFrame(arr, columns=['X', 'Y']) # Show the DataFrame print('DataFrame from NumPy array:') print(df) # Convert DataFrame back to NumPy array arr_back = df.values print('\nNumPy array from DataFrame:') print(arr_back) # Use NumPy function on DataFrame column mean_x = np.mean(df['X']) print(f'\nMean of column X: {mean_x}')
OutputSuccess
Important Notes
Using df.values returns the underlying NumPy array of a DataFrame.
Many NumPy functions work directly on Pandas Series and DataFrames.
When converting, column names and indexes are lost if you only use NumPy arrays.
Summary
Pandas DataFrames use NumPy arrays to store data efficiently.
You can convert between Pandas and NumPy easily for flexible data analysis.
NumPy functions can be applied directly to Pandas data.