How to Create a Pandas DataFrame Object in Python



Python


In this article, we show how to create a pandas dataframe object in Python.

A dataframe object is an object composed of a number of pandas series.

A pandas series is a labeled list of data.

A dataframe object is an object made up of a number of series objects.

A dataframe object is most similar to a table. It is composed of rows and columns.

We create a dataframe object with the Dataframe keyword.

In our example below we create a 4x3 Dataframe object, one which has 4 rows and 3 columns.

We populate the DataFrame using random values.

This is shown in the following code below.



So let's now go over the code.

So we first have to import the pandas module. We do this with the line, import pandas as pd.

as pd means that we can reference the pandas module with pd instead of writing out the full pandas each time.

We import rand from numpy.random, so that we can populate the DataFrame with random values. In other words, we won't need to manually create the values in the table. The randn function will populate it with random values.

We create a variable, dataframe1, which we set equal to, pd.DataFrame(randn(4,3),['A','B','C','D',],['X','Y','Z'])

This creates a DataFrame object with 4 rows and 3 columns.

The rows are 'A', 'B', 'C', and 'D'.

The columns are 'X', 'Y', and 'Z'.

After we output the dataframe1 object, we get the DataFrame object with all the rows and columns, which you can see above.

We then use the type() function to show the type of object it is, which is,

So this is all that is required to create a pandas dataframe object in Python.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...