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?

<div><p>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.</p>
<figure class="wp-block-image size-large is-resized is-style-rounded"><img loading="lazy" src="https://blog.finxter.com/wp-content/uploads/2020/05/erol-ahmed-aIYFR0vbADk-unsplash-1024x512.jpg" alt="Photo by Erol Ahmed on Unsplash" class="wp-image-8890" width="634" height="317" srcset="https://blog.finxter.com/wp-content/uploads/2020/05/erol-ahmed-aIYFR0vbADk-unsplash-scaled.jpg 1024w, https://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" /></figure>
<h2>Create a DataFrame From a CSV File</h2>
<p>Creating DataFrames with the function <em>pd.read_csv(filename)</em> is probably the best known.<br />The first line of the csv file contains the column labels separated by commas.<br />In the following lines follow the data points, in each row as many as there are columns.<br />The data points must be separated by commas, if you want to use the default settings of <em>pd.read_csv()</em>.<br />Here is an example of such a csv file:</p>
<pre class="wp-block-code"><code># data.csv column1, column2, column3
value00, value01, value02
value10, value11, value12
value20, value21, value22</code></pre>
<p>The following code snippet creates a DataFrame from the data.csv file:</p>
<pre class="wp-block-code"><code>import pandas as pd df = pd.read_csv('data.csv')</code></pre>
<p>The function <em>pd.read_table()</em> is similar but expects tabs as delimiters instead of comas.<br />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.<br />To do so, use the parameter <em>index_col</em>. Example: pd.read_csv(‘data.csv’, index_col=0)</p>
<h2>Create a DataFrame From a List of Lists</h2>
<p>A DataFrame can be created from a list of lists where each list in the outer list contains the data for one row.<br />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:</p>
<pre class="wp-block-code"><code>import pandas as pd data = [ ['Bob', 23], ['Carl', 34], ['Dan', 14]
]
df = pd.DataFrame(data, columns=['Name', 'Age'])</code></pre>
<h2>Create a DataFrame From a Dictionary of Lists</h2>
<p>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.</p>
<pre class="wp-block-code"><code>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)</code></pre>
<h2>Create a DataFrame From a List of Dictionaries</h2>
<p>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.</p>
<pre class="wp-block-code"><code>data = [ {'Car':'Mercedes', 'Driver':'Hamilton, Lewis'}, {'Car':'Ferrari', 'Driver':'Schumacher, Michael'}, {'Car':'Lamborghini', 'Driver':'Rossi, Semino'}
]</code></pre>
<h2>Create a DataFrame From a List of Tuples</h2>
<p>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 <em>columns</em>.</p>
<pre class="wp-block-code"><code>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'])</code></pre>
<h2>Summing Up</h2>
<p>In this article we have gone through a range of different ways to create DataFrames in pandas. However, it is not exhaustive.<br />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.</p>
<p>The post <a href="https://blog.finxter.com/how-to-create-a-dataframe-in-pandas/" target="_blank" rel="noopener noreferrer">How to Create a DataFrame in Pandas?</a> first appeared on <a href="https://blog.finxter.com/" target="_blank" rel="noopener noreferrer">Finxter</a>.</p>
</div>


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



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016