In Data Science, the most used data structures are the Series and the DataFrame which deal with arrays and tabular data respectively. In this article, I will walk you through a tutorial on pandas DataFrame with Python.
What is a DataFrame in Pandas?
The two-dimensional counterpart of the one-dimensional series is known as the DataFrame in Pandas. If you think a data block is row-oriented, the interface will feel bad.
Also, Read – 100+ Machine Learning Projects Solved and Explained.
Many tabular data structures are row-oriented. This may be due to spreadsheets and CSV files that are processed line by line. This may be due to the many OLTP databases that are row-oriented right out of the box. A DataFrame is often used for analytical purposes and is best understood when viewed as column-oriented, where each column is a series.
The Pandas DataFrame can be created from many types of inputs such as:
- columns (dictionaries of lists)
- rows (list of dictionaries)
- CSV file (pd.read_csv)
- by NumPy ndarray
- And more, SQL, HDF5, etc.
Pandas DataFrame with Python
Below is a simple attempt to create a DataFrame with Python as a column-oriented data structure without using Pandas. It has an integer index based on 0, but it is not mandatory, the index can also be based on a string:
df = {'index':[0, 1, 2], 'col':[{'name':'growth', 'data':[.5, .7, 1.2]}, { 'name':'Name', 'data':['Paul', 'George', 'Ringo'] },]} print(df)
{‘index’: [0, 1, 2], ‘col’: [{‘name’: ‘growth’, ‘data’: [0.5, 0.7, 1.2]}, {‘name’: ‘Name’, ‘data’: [‘Paul’, ‘George’, ‘Ringo’]}]}
In a Pandas DataFrame, rows are accessible through the index and columns are accessible from the column name. Now let’s use the pandas DataFrame object to create the same data structure:
import pandas as pd df = pd.DataFrame({'growth':[.5, .7, 1.2],'Name':["Paul", "George", "Ringo"]}) print(df)
growth Name 0 0.5 Paul 1 0.7 George 2 1.2 Ringo
I hope you liked this article on how to create a DataFrame in the Pandas library in Python. Feel free to ask your valuable questions in the comments section below.