You might need to convert your numpy arrays into a DataFrame. Or maybe you have a list of values (while working in vanilla python) and you want to turn them into a DataFrame within Pandas. That is where list to dataframe comes into play.
I do this most often when I’m iterating through a for loop, saving the values, and then need to create a new DataFrame from them. Important: When passing multiple lists, you must pass lists that represent rows.
Pseudo Code: With your python lists, turn them into a pandas DataFrame
Pandas List To DataFrame Parameters
Since creating a DataFrame from a list uses the standard pd.DataFrame(), you only have to worry about those parameters
- data = The data that you want your DataFrame to be created from. In this case, it is going to be the list(s) you pass. Each list represents a row in your future DataFrame.
- index = You can pass an index that is the same length as the number of lists you pass. Think of index as row labels
- columns = Your column names. The length of this list of columns must match the number of items in your child lists.
Now the fun part, let’s take a look at a code sample
import pandas as pd
Pandas List To DataFrame¶
You may want to create a DataFrame from a list or list of lists. In this case, all you need to do is call the general pd.DataFrame() function and pass your data.
We will run through 3 examples:
- Creating a DataFrame from a single list
- Creating a DataFrame from multiple lists
- Creating a DataFrame from multiple lists and specifying column names
- Creating a DataFrame from multiple lists and specifying column names and index
But first, let's create some lists
list1 = ["Foreign Cinema", 50] list2 = ["Liho Liho", 45] list3 = ["500 Club", 102] list4 = ["The Square", 65]
1. Creating a DataFrame from a single list¶
To start off, let's create a DataFrame from a single list. Do do this I'm going to call pd.DataFrame, then pass data=my_list.
You can see, when I pass one list, pandas returns a single column DataFrame. The list values are the row within a single column.
2. Creating a DataFrame from multiple lists¶
If I wanted to create a DataFrame with multiple rows (I do this most of the time), then you just need to pass a list of lists in to data
pd.DataFrame(data=[list1, list2, list3, list4])
3. Creating a DataFrame from multiple lists and specifying column names¶
Next, let's specify column names. Pass your column names to the column parameter. Make sure that the number of columns you specify equals the number of columns in your DataFrame.
pd.DataFrame(data=[list1, list2, list3, list4], columns=['Name', 'num_customers'])
4. Creating a DataFrame from multiple lists and specifying column names and index¶
Finally, let's create a DataFrame with a specific index. To do this I'll pass a list of index labels to the index parameter.
pd.DataFrame(data=[list1, list2, list3, list4], columns=['Name', 'num_customers'], index=['res1', 'res2', 'res3', 'res4'])
Check out more Pandas functions on our Pandas Page