python-scipyHow do I convert a Python Numpy array to a Pandas Dataframe?
Converting a Python Numpy array to a Pandas Dataframe is a simple task. The following example code demonstrates how to do this:
import pandas as pd
import numpy as np
# Create a Numpy array
data = np.array([['','Col1','Col2'],
['Row1',1,2],
['Row2',3,4]])
# Create a Pandas Dataframe
df = pd.DataFrame(data=data[1:,1:],
index=data[1:,0],
columns=data[0,1:])
print(df)
Output example
Col1 Col2
Row1 1 2
Row2 3 4
The code above consists of the following parts:
import pandas as pdandimport numpy as np- imports the necessary libraries for the task.data = np.array([['','Col1','Col2'], ['Row1',1,2], ['Row2',3,4]])- creates a Numpy array with the data we want to convert.df = pd.DataFrame(data=data[1:,1:], index=data[1:,0], columns=data[0,1:])- creates a Pandas Dataframe from the Numpy array.print(df)- prints the Dataframe.
For more information, please refer to the following links:
More of Python Scipy
- How do I create a 2D array of zeros using Python and NumPy?
- How can I use Python and SciPy to find the zeros of a function?
- How do I use Scipy zeros in Python?
- How do I use Python Numpy to read and write Excel (.xlsx) files?
- How can I use Python and SciPy to calculate a binomial distribution?
- How can I use Python Scipy to zoom in on an image?
- How do I calculate the cross-correlation of two arrays using Python and NumPy?
- How do I create a numpy array of zeros using Python?
- How do I download a Python Scipy .whl file?
- How can I use Python and NumPy to perform a XOR operation?
See more codes...