Create a DataFrame with customized index parameters in Pandas

PandasServer Side ProgrammingProgramming

To create a DataFrame with some index, we can pass a list of values and assign them into index in DataFrame Class.

Steps

  • Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.

  • Put a list of indices in the index of DataFrame class.

  • Print the DataFrame with the customized index.

Example

 Live Demo

import pandas as pd

df = pd.DataFrame(
   {
      "x": [5, 2, 1, 9],
      "y": [4, 1, 5, 10],
      "z": [4, 1, 5, 0]
   }
)
print "Input DataFrame is:
", df df = pd.DataFrame(    {       "x": [5, 2, 1, 9],       "y": [4, 1, 5, 10],       "z": [4, 1, 5, 0]    },    index=["John", "Jacob", "Ally", "Simon"] ) print "With Customized Index:
", df

Output

Input DataFrame is:
   x  y  z
0  5  4  4
1  2  1  1
2  1  5  5
3  9  10 0

With Customized Index:
       x  y   z
John   5  4   4
Jacob  2  1   1
Ally   1  5   5
Simon  9  10  0
raja
Updated on 30-Aug-2021 11:51:23

Advertisements