Pandas Series to Numpy Array
In data analysis and machine learning tasks, it is common to work with both Pandas and Numpy libraries in Python. Pandas is used for data manipulation and analysis, while Numpy is used for scientific computing. In some cases, you may need to convert a Pandas Series to a Numpy array to perform certain operations. In this article, we will explore how to convert a Pandas Series to a Numpy array.
Importing the required libraries
Before we move on to converting Pandas Series to Numpy array, let’s import the required libraries – Pandas and Numpy.
import pandas as pd
import numpy as np
Creating a Pandas Series
Let’s create a sample Pandas Series to work with.
import pandas as pd
import numpy as np
data = pd.Series([10, 20, 30, 40, 50])
print(data)
Output:
Converting Pandas Series to Numpy array
To convert a Pandas Series to a Numpy array, you can simply use the values
attribute of the Series.
import pandas as pd
import numpy as np
data = pd.Series([10, 20, 30, 40, 50])
numpy_array = data.values
print(numpy_array)
Output:
Checking the type of the converted array
Let’s check the type of the converted Numpy array.
import pandas as pd
import numpy as np
data = pd.Series([10, 20, 30, 40, 50])
numpy_array = data.values
print(type(numpy_array))
Output:
Performing operations on the Numpy array
Now that we have converted the Pandas Series to a Numpy array, we can perform various operations on it.
import pandas as pd
import numpy as np
data = pd.Series([10, 20, 30, 40, 50])
numpy_array = data.values
mean = np.mean(numpy_array)
print("Mean:", mean)
std_dev = np.std(numpy_array)
print("Standard Deviation:", std_dev)
Output:
Convert a Pandas Series with missing values to a Numpy array
If your Pandas Series contains missing values, you can handle them by converting it to a Numpy array.
import pandas as pd
import numpy as np
data = pd.Series([10, 20, 30, 40, 50])
numpy_array = data.values
data_with_na = pd.Series([10, np.nan, 30, 40, 50])
numpy_array_with_na = data_with_na.values
print(numpy_array_with_na)
Output:
Handling missing values in the Numpy array
You can handle missing values in the Numpy array using Numpy’s functions.
import pandas as pd
import numpy as np
data = pd.Series([10, 20, 30, 40, 50])
numpy_array = data.values
data_with_na = pd.Series([10, np.nan, 30, 40, 50])
numpy_array_with_na = data_with_na.values
cleaned_array = np.nan_to_num(numpy_array_with_na)
print(cleaned_array)
Output:
Convert a Pandas Series with strings to a Numpy array
If your Pandas Series contains strings, you can convert it to a Numpy array as well.
import pandas as pd
import numpy as np
data = pd.Series([10, 20, 30, 40, 50])
numpy_array = data.values
data_strings = pd.Series(['Web', 'numpywhere.com', 'deepinout.com', 'geek-docs.com'])
numpy_array_strings = data_strings.values
print(numpy_array_strings)
Output:
Convert a Pandas Series with datetime values to a Numpy array
You can also convert a Pandas Series containing datetime values to a Numpy array.
import pandas as pd
import numpy as np
data = pd.Series([10, 20, 30, 40, 50])
numpy_array = data.values
dates = pd.date_range('2024-01-01', periods=5)
numpy_array_dates = dates.values
print(numpy_array_dates)
Output:
Using the Numpy array in further analysis
Once you have converted the Pandas Series to a Numpy array, you can use it in further analysis, visualization, or machine learning tasks.
This concludes our exploration of converting Pandas Series to Numpy array. By following the steps mentioned above, you can easily convert a Pandas Series to a Numpy array and perform various operations on it.