Create a DataFrame from a Numpy array and specify the index column and column headers

Create a DataFrame from a Numpy array and specify the index column and column headers

You can create a DataFrame from a NumPy array using the pandas library. Here's how you can do this while specifying the index and column headers:

  • First, make sure you have both numpy and pandas installed:
pip install numpy pandas 
  • Here's an example of how to create a DataFrame from a NumPy array, specifying index and columns:
import numpy as np import pandas as pd # Create a NumPy array data = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) # Specify column headers and index columns = ['Column_A', 'Column_B', 'Column_C'] index = ['Row_1', 'Row_2', 'Row_3'] # Create a DataFrame df = pd.DataFrame(data=data, index=index, columns=columns) print(df) 

Output:

 Column_A Column_B Column_C Row_1 1 2 3 Row_2 4 5 6 Row_3 7 8 9 

The index parameter allows you to specify row labels, and the columns parameter allows you to specify column headers. Adjust these parameters as necessary for your data.


More Tags

jquery-ui-tabs svg-filters menuitem configuration-files cloud9-ide grafana tmx interruption kingfisher orc

More Programming Guides

Other Guides

More Programming Examples