DataFrames in Pandas

The Pandas DataFrame is a Two-dimensional tabular data structure i.e. table with rows and columns. The DataFrame() method is used for this purpose and has the following parameters:

  • data: The data to be stored in the Pandas DataFrame
  • index: The index values to be provided for the resultant frame.
  • columns: Set the column labels for the resultant frame if data does not mention before
  • dtype: It is the datatype and only a single type is allowed.
  • copy: To copy the input data

In this lesson, we will see some examples to:

  • Create a Pandas DataFrame
  • Access a group of rows or columns in a Pandas DataFrame
  • Access a group of rows or columns by integer positions in a Pandas DataFrame
  • Name your own indexes in a Pandas DataFrame
  • Iterating a DataFrame

Create a Pandas DataFrame

To create a dataframe in pandas, use the pandas.DataFrame() method. Let us see an example wherein we have student records:

Output

The 0, 1, 2, etc. are the index or label that gets automatically added to the table.

Access a group of rows or columns in a Pandas DataFrame

The dataframe.loc is used in Pandas to access a group of rows or columns in a DataFrame. Let us see an example:

Output

Access a group of rows or columns by integer positions in a Pandas DataFrame

The dataframe.iloc is used to access a group of rows or columns by integers. We have also set columns and indexes. Let us see an example:

Output

Name your indexes in a Pandas DataFrame

The index argument is used to set and name your indexes in a DataFrame. Let us see an example:

Output

Iterate a DataFrame

To iterate a DataFrame and display the column names, use the for loop as in the below example:

Output

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:


For Videos, Join Our YouTube Channel: Join Now


Read More:

How to Install Pandas on Windows
Series in Pandas
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading