Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Create a DataFrame in Pandas?

#1
How to Create a DataFrame in Pandas?

In Python’s pandas module, DataFrames are two-dimensional data objects. You can think of them as tables with rows and columns that contain data. This article provides an overview of the most common ways to instantiate DataFrames. We follow the convention to rename the pandas import to pd.

Photo by Erol Ahmed on Unsplashhttps://blog.finxter.com/wp-content/uplo...00x150.jpg 300w, https://blog.finxter.com/wp-content/uplo...68x384.jpg 768w" sizes="(max-width: 634px) 100vw, 634px" />

Create a DataFrame From a CSV File


Creating DataFrames with the function pd.read_csv(filename) is probably the best known.
The first line of the csv file contains the column labels separated by commas.
In the following lines follow the data points, in each row as many as there are columns.
The data points must be separated by commas, if you want to use the default settings of pd.read_csv().
Here is an example of such a csv file:

# data.csv column1, column2, column3
value00, value01, value02
value10, value11, value12
value20, value21, value22

The following code snippet creates a DataFrame from the data.csv file:

import pandas as pd df = pd.read_csv('data.csv')

The function pd.read_table() is similar but expects tabs as delimiters instead of comas.
The default behavior of pandas adds an integer row index, yet it is also possible to choose one of the data columns to become the index column.
To do so, use the parameter index_col. Example: pd.read_csv(‘data.csv’, index_col=0)

Create a DataFrame From a List of Lists


A DataFrame can be created from a list of lists where each list in the outer list contains the data for one row.
To create the DataFrame we use the DataFrame’s constructor to which we pass the list of list and a list with the column labels:

import pandas as pd data = [ ['Bob', 23], ['Carl', 34], ['Dan', 14]
]
df = pd.DataFrame(data, columns=['Name', 'Age'])

Create a DataFrame From a Dictionary of Lists


A DataFrame can be created from a dictionary of lists. The dictionary’s keys are the column labels, the lists contain the data for the columns.

import pandas as pd # columns
names = ['Alice', 'Bob', 'Carl']
ages = [21, 27, 35] # create the dictionary of lists
data = {'Name':names, 'Age':ages} df = pd.DataFrame(data)

Create a DataFrame From a List of Dictionaries


A DataFrame can be created from a list of dictionaries. Each dictionary represents a row in the DataFrame. The keys in the dictionaries are the column labels and the values are the values for the columns.

data = [ {'Car':'Mercedes', 'Driver':'Hamilton, Lewis'}, {'Car':'Ferrari', 'Driver':'Schumacher, Michael'}, {'Car':'Lamborghini', 'Driver':'Rossi, Semino'}
]

Create a DataFrame From a List of Tuples


The DataFrame constructor can also be called with a list of tuples where each tuple represents a row in the DataFrame. In addition we pass a list of column labels to the parameter columns.

import pandas as pd names = ['Alice', 'Bob', 'Clarisse', 'Dagobert']
ages = [20, 53, 42, 23] # create a list of tuples
data = list(zip(names, ages)) df = pd.DataFrame(data, columns=['Name', 'Age'])

Summing Up


In this article we have gone through a range of different ways to create DataFrames in pandas. However, it is not exhaustive.
You should choose the method which best fits your use-case, this is to say, the method which requires the least amount of data transformation.

The post How to Create a DataFrame in Pandas? first appeared on Finxter.



https://www.sickgaming.net/blog/2020/11/...in-pandas/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] How Does Pandas Concat Work? xSicKxBot 0 9 11-23-2020, 09:03 AM
Last Post: xSicKxBot
  [Tut] How to Fix “ImportError: No module named pandas” [Mac/Linux/Windows/PyCharm] xSicKxBot 0 30 11-08-2020, 04:36 AM
Last Post: xSicKxBot
  [Tut] Pandas NaN — Working With Missing Data xSicKxBot 0 30 11-05-2020, 02:44 AM
Last Post: xSicKxBot
  [Tut] A Simple Recommendation System With Pandas xSicKxBot 0 24 10-31-2020, 06:58 AM
Last Post: xSicKxBot
  [Tut] How to Create a Singleton in Python? xSicKxBot 0 53 09-10-2020, 08:54 PM
Last Post: xSicKxBot
  [Tut] How to Create a Database Table with pyodbc in Python xSicKxBot 0 100 08-03-2020, 08:37 AM
Last Post: xSicKxBot
  [Tut] How to Create a List of Dictionaries in Python? xSicKxBot 0 114 07-09-2020, 11:37 AM
Last Post: xSicKxBot
  [Tut] Create a List of Random Numbers — The Most Pythonic Way xSicKxBot 0 119 07-07-2020, 04:23 PM
Last Post: xSicKxBot
  [Tut] How to Create a Python List of Size n? xSicKxBot 0 142 07-06-2020, 12:09 PM
Last Post: xSicKxBot
  [Tut] Tilde Python Pandas DataFrame xSicKxBot 0 156 05-17-2020, 06:40 AM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
Microsoft - The human side of AI for che...
Last Post: xSicKxBot
Today 12:11 PM
» Replies: 0
» Views: 4
News - Red Dead Online Standalone Is Out...
Last Post: xSicKxBot
Today 12:10 PM
» Replies: 0
» Views: 37
(Indie Deal) Ancestors, The Walking Dead...
Last Post: xSicKxBot
Today 10:35 AM
» Replies: 0
» Views: 4
News - Atelier Ryza Developer Clarifies ...
Last Post: xSicKxBot
Today 05:30 AM
» Replies: 0
» Views: 5
News - Doom Eternal Update 4 Adds New Ma...
Last Post: xSicKxBot
Today 05:30 AM
» Replies: 0
» Views: 97
Unreal Engine Asset Giveaway For Decembe...
Last Post: xSicKxBot
Today 04:15 AM
» Replies: 0
» Views: 7
[Tut] Searching The Parse Tree Using Bea...
Last Post: xSicKxBot
Yesterday 10:53 PM
» Replies: 0
» Views: 9
News - PixelJunk Eden 2 Coming Very Soon
Last Post: xSicKxBot
Yesterday 10:53 PM
» Replies: 0
» Views: 124
(Indie Deal) Black Friday Weekend Round-...
Last Post: xSicKxBot
Yesterday 08:48 PM
» Replies: 0
» Views: 9
Xbox Wire - How to Buy a Digital Game as...
Last Post: xSicKxBot
Yesterday 06:00 PM
» Replies: 0
» Views: 9

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020