How does a data table in pandas represent?


To represent a data table in pandas we have a table-like object in pandas which is DataFrame. A DataFrame is a 2-dimensional data structure in pandas and those data structures can store any kind of data in column and row wise representation.

Example

df = pd.DataFrame({"Name": [ "Harris","William","Elizabeth",],"Age": [22, 35, 58],"Sex": ["male", "male", "female"],})
print(df)

Explanation

Here we created a data table in pandas manually by using the DataFrame object and the data is a dictionary of lists. While creating the tabular data we only mentioned the column labels but yet mentioned any row labels (index value). But you can see the label values in the output block below.

Output

       Name Age  Gender
0    Harris 22   male
1   William 35   male
2 Elizabeth 58 female

In the output data table, there are a total of 3 columns labeled with Name, Age, and Gender names as well as there are 3 rows labeled with 0,1,2 index values. These index values are automatically created values and the column names are explicitly defined by use(keys names in our dictionary as column names).

We can see that the data table created by Pandas DataFrame would look very similar to the spreadsheet and SQL table.

And each column in a DataFrame is a pandas Series object. it only has one column and multiple rows with index labels.

Example

print(df.Name)

Explanation

We can get a single column name from our DataFrame (df) by using df.name of column syntax. And resultant pandas Series object can be seen below.

Output

0    Harris
1   William
2 Elizabeth
Name: Name, dtype: object

The 3 rows data from the name column of our tabular data (df). The data type of this series is object dtype.

In this way, pandas will represent the tabular data using the pandas DataFrame object.

Updated on: 18-Nov-2021

306 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements