{"id":130598,"date":"2022-12-17T08:39:56","date_gmt":"2022-12-17T08:39:56","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=985131"},"modified":"2022-12-17T08:39:56","modified_gmt":"2022-12-17T08:39:56","slug":"how-to-create-a-dataframe-from-lists","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/12\/17\/how-to-create-a-dataframe-from-lists\/","title":{"rendered":"How to Create a DataFrame From Lists?"},"content":{"rendered":"\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-top\" data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;985131&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<p>Pandas is a great library for data analysis in Python. With Pandas, you can create visualizations, filter rows or columns, add new columns, and save the data in a wide range of formats. The workhorse of Pandas is the <strong>DataFrame<\/strong>. <\/p>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Recommended<\/strong>: <a href=\"https:\/\/blog.finxter.com\/pandas-quickstart\/\" data-type=\"post\" data-id=\"16511\" target=\"_blank\" rel=\"noreferrer noopener\">10 Minutes to Pandas (in 5 Minutes)<\/a><\/p>\n<p>So the first step working with Pandas is often to get our data into a DataFrame. If we have data stored in <a href=\"https:\/\/blog.finxter.com\/python-lists\/\" data-type=\"post\" data-id=\"7332\" target=\"_blank\" rel=\"noreferrer noopener\">lists<\/a>, how can we create this all-powerful DataFrame? <\/p>\n<p>There are 4 basic strategies:<\/p>\n<ol type=\"1\">\n<li>Create a <a href=\"https:\/\/blog.finxter.com\/python-dictionary\/\" data-type=\"post\" data-id=\"5232\" target=\"_blank\" rel=\"noreferrer noopener\">dictionary<\/a> with column names as keys and your lists as values. Pass this dictionary as an argument when creating the DataFrame.<\/li>\n<li>Pass your lists into the <code><a href=\"https:\/\/blog.finxter.com\/python-ziiiiiiip-a-helpful-guide\/\" data-type=\"post\" data-id=\"1938\" target=\"_blank\" rel=\"noreferrer noopener\">zip()<\/a><\/code> function. As with strategy 1, your lists will become columns in the DataFrame.<\/li>\n<li>Put your lists into a list instead of a dictionary. In this case, your lists become rows instead of columns.<\/li>\n<li><a href=\"https:\/\/blog.finxter.com\/how-to-create-a-dataframe-in-pandas\/\" data-type=\"post\" data-id=\"16764\" target=\"_blank\" rel=\"noreferrer noopener\">Create an empty DataFrame<\/a> and add columns one by one.<\/li>\n<\/ol>\n<h2>Method 1: Create a DataFrame using a Dictionary<\/h2>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1010\" height=\"645\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-237.png\" alt=\"\" class=\"wp-image-985155\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-237.png 1010w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-237-300x192.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-237-768x490.png 768w\" sizes=\"auto, (max-width: 1010px) 100vw, 1010px\" \/><\/figure>\n<\/div>\n<p>The first step is to import pandas. If you haven&#8217;t already, <a href=\"https:\/\/blog.finxter.com\/how-to-install-pandas-in-python\/\" data-type=\"post\" data-id=\"35926\" target=\"_blank\" rel=\"noreferrer noopener\">install pandas<\/a> first.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pandas as pd<\/pre>\n<p>Let&#8217;s say you have employee data stored as lists.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># if your data is stored like this\nemployee = ['Betty', 'Veronica', 'Archie', 'Jughead']\nsalary = [110_000, 20_000, 80_000, 70_000]\nbonus = [1000, 500, 2500, 400]\ntax_rate = [.1, .25, .17, .4]\nabsences = [0, 1, 0, 52]\n<\/pre>\n<p>Build a dictionary using column names as keys and your lists as values.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># you can easily create a dictionary that will define your dataframe\nemp_data = { 'name': employee, 'salary': salary, 'bonus': bonus, 'tax_rate': tax_rate, 'absences': absences\n}\n<\/pre>\n<p>Your lists will become columns in the resulting DataFrame.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"367\" height=\"164\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-230.png\" alt=\"\" class=\"wp-image-985144\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-230.png 367w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-230-300x134.png 300w\" sizes=\"auto, (max-width: 367px) 100vw, 367px\" \/><\/figure>\n<\/div>\n<h2>Create a DataFrame using the zip function<\/h2>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"1010\" height=\"668\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-238.png\" alt=\"\" class=\"wp-image-985156\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-238.png 1010w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-238-300x198.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-238-768x508.png 768w\" sizes=\"auto, (max-width: 1010px) 100vw, 1010px\" \/><\/figure>\n<\/div>\n<p>Pass each list as a separate argument to the <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-ziiiiiiip-a-helpful-guide\/\" data-type=\"post\" data-id=\"1938\" target=\"_blank\">zip()<\/a><\/code> function. You can specify the column names using the <code>columns<\/code> parameter or by setting the <code>columns<\/code> property on a separate line.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">emp_df = pd.DataFrame(zip(employee, salary, bonus, tax_rate, absences))\nemp_df.columns = ['name', 'salary', 'bonus', 'tax_rate', 'absences']\n<\/pre>\n<p>The <code>zip()<\/code> function creates an <a href=\"https:\/\/blog.finxter.com\/iterators-iterables-and-itertools\/\" data-type=\"post\" data-id=\"29507\" target=\"_blank\" rel=\"noreferrer noopener\">iterator<\/a>. For the first iteration, it grabs every value at index 0 from each list. This becomes the first row in the DataFrame. Next, it grabs every value at index 1 and this becomes the second row. This continues until it exhausts the shortest list.<\/p>\n<p>We can loop thru the iterator to see how this works.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">i = 0\nfor value in zip(employee, salary, bonus, tax_rate, absences): print(f'zipped value at index {i}: {value}') i += 1\n<\/pre>\n<p>Each of these values becomes a row in the DataFrame:<\/p>\n<pre class=\"wp-block-preformatted\"><code>zipped value at index 0: ('Betty', 110000, 1000, 0.1, 0)\nzipped value at index 1: ('Veronica', 20000, 500, 0.25, 1)\nzipped value at index 2: ('Archie', 80000, 2500, 0.17, 0)\nzipped value at index 3: ('Jughead', 70000, 400, 0.4, 52)<\/code>\n<\/pre>\n<h2>Create a DataFrame using a list of lists<\/h2>\n<p>What if you have a separate list for each employee? In this case, we can just create a <a href=\"https:\/\/blog.finxter.com\/python-list-of-lists\/\" data-type=\"post\" data-id=\"7890\" target=\"_blank\" rel=\"noreferrer noopener\">list of lists<\/a>. Each of the inner lists becomes a row in the DataFrame.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># lists for employees instead of features\nbetty = ['Betty', 110000, 1000, 0.1, 0]\nveronica = ['Veronica', 20000, 500, 0.25, 1]\narchie = ['Archie', 80000, 2500, 0.17, 0]\njughead = ['Jughead', 70000, 400, 0.4, 52] emp_df = pd.DataFrame([betty, veronica, archie, jughead])\nemp_df.columns = ['name', 'salary', 'bonus', 'tax_rate', 'absences']\nemp_df\n<\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"380\" height=\"158\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-231.png\" alt=\"\" class=\"wp-image-985145\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-231.png 380w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-231-300x125.png 300w\" sizes=\"auto, (max-width: 380px) 100vw, 380px\" \/><\/figure>\n<\/div>\n<h2>Create a DataFrame using a list of dictionaries<\/h2>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"856\" height=\"863\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-239.png\" alt=\"\" class=\"wp-image-985157\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-239.png 856w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-239-298x300.png 298w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-239-150x150.png 150w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-239-768x774.png 768w\" sizes=\"auto, (max-width: 856px) 100vw, 856px\" \/><\/figure>\n<\/div>\n<p>If the employee data is stored in dictionaries instead of lists, we use a list of dictionaries.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">betty = {'name': 'Betty', 'salary': 110000, 'bonus': 1000, 'tax_rate': 0.1, 'absences': 0} veronica = {'name': 'Veronica', 'salary': 20000, 'bonus': 500, 'tax_rate': 0.25, 'absences': 1} archie = {'name': 'Archie', 'salary': 80000, 'bonus': 2500, 'tax_rate': 0.17, 'absences': 0} jughead = {'name': 'Jughead', 'salary': 70000, 'bonus': 400, 'tax_rate': 0.4, 'absences': 52} pd.DataFrame([betty, veronica, archie, jughead])<\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"374\" height=\"159\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-232.png\" alt=\"\" class=\"wp-image-985146\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-232.png 374w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-232-300x128.png 300w\" sizes=\"auto, (max-width: 374px) 100vw, 374px\" \/><\/figure>\n<\/div>\n<p>The columns are determined by the keys in the dictionaries. What if the dictionaries don\u2019t all have the same keys?<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">betty = {'name': 'Betty', 'salary': 110000, 'bonus': 1000, 'tax_rate': 0.1, 'absences': 0, 'hire_date': '2001-01-01'} veronica = {'name': 'Veronica', 'salary': 20000, 'bonus': 500, 'tax_rate': 0.25, 'absences': 1} archie = {'name': 'Archie', 'salary': 80000, 'bonus': 2500, 'tax_rate': 0.17, 'absences': 0, 'title': 'Vice Chief Leader'} jughead = {'name': 'Jughead', 'salary': 70000, 'bonus': 400, 'tax_rate': 0.4, 'absences': 52, 'rank': 'yes'} pd.DataFrame([betty, veronica, archie, jughead])\n<\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"624\" height=\"151\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-233.png\" alt=\"\" class=\"wp-image-985147\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-233.png 624w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-233-300x73.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n<\/div>\n<p>All of the keys will be used. Anytime pandas encounters a dictionary with a missing key, the missing value will be replaced with NaN which stands for \u2018not a number\u2019.<\/p>\n<h2>Create an empty DataFrame and add columns one by one<\/h2>\n<p>This method might be preferable if you needed to create a lot of new calculated columns. Here we create a new column for after-tax income.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">emp_df = pd.DataFrame()\nemp_df['name'] = employee\nemp_df['salary'] = salary\nemp_df['bonus'] = bonus\nemp_df['tax_rate'] = tax_rate\nemp_df['absences'] = absences income = emp_df['salary'] + emp_df['bonus']\nemp_df['after_tax'] = income * (1 - emp_df['tax_rate'])\n<\/pre>\n<h2>How to add a list to an existing DataFrame<\/h2>\n<p>Here is a neat trick. If you want to edit a row in a DataFrame you can use the handy <code><a href=\"https:\/\/blog.finxter.com\/slicing-data-from-a-pandas-dataframe-using-loc-and-iloc\/\" data-type=\"post\" data-id=\"230997\" target=\"_blank\" rel=\"noreferrer noopener\">loc<\/a><\/code> method. Loc allows you to access rows and columns by their index value.<\/p>\n<p>To access a row:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">emp_df.loc[3]<\/pre>\n<p>Output is the row with index value 3 as a Series:<\/p>\n<pre class=\"wp-block-preformatted\"><code>name Jughead\nsalary 70000\nbonus 400\ntax_rate 0.4\nabsences 52\nName: 3, dtype: object<\/code>\n<\/pre>\n<p>To access a column just pass in the column name as the index. Note that we have to specify the row and column indexes. The format is <code>[rows, columns]<\/code>. If you want all rows you can use \u201c<code>:<\/code>\u201d as we do here. The <code>:<\/code> also works if you want all columns.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">emp_df.loc[:, 'salary']<\/pre>\n<p>Output is also a series<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">0 110000\n1 20000\n2 80000\n3 70000\n4 200000\nName: salary, dtype: int64\n<\/pre>\n<p>So how do we use <code>loc<\/code> to add a new row? If we use a row index that doesn\u2019t exist in the DataFrame, it will create a new row for us.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">new_emp = ['Fonzie', 200000, 30000, .05, 112]\nemp_df.loc[4] = new_emp\nemp_df\n<\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"366\" height=\"183\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-234.png\" alt=\"\" class=\"wp-image-985148\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-234.png 366w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-234-300x150.png 300w\" sizes=\"auto, (max-width: 366px) 100vw, 366px\" \/><\/figure>\n<\/div>\n<p>You can also update existing data with <code>loc<\/code>. Let\u2019s drop Fonzie\u2019s salary. It looks a bit excessive.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">emp_df.loc[4, 'salary'] = 105000\nemp_df\n<\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"376\" height=\"183\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-235.png\" alt=\"\" class=\"wp-image-985149\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-235.png 376w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-235-300x146.png 300w\" sizes=\"auto, (max-width: 376px) 100vw, 376px\" \/><\/figure>\n<\/div>\n<p>That\u2019s more like it.<\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>There are many different ways of creating a DataFrame. We looked at several methods using data stored in lists. Each will get the job done. <\/p>\n<p>The most convenient method will depend on what your lists represent. <\/p>\n<p>If each of your lists would best be represented as a column, then a dictionary of lists might be the easiest way to go. <\/p>\n<p>If each of your lists would best be represented as a row, then a list of lists would be a good choice. <\/p>\n<p>To add data in a list as a new row in an existing DataFrame, the <code>loc<\/code> method comes in handy. Loc is also useful for updating existing data.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Pandas is a great library for data analysis in Python. With Pandas, you can create visualizations, filter rows or columns, add new columns, and save the data in a wide range of formats. The workhorse of Pandas is the DataFrame. Recommended: 10 Minutes to Pandas (in 5 Minutes) So the first [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-130598","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/130598","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=130598"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/130598\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=130598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=130598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=130598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}