Pandas functions
Pandas List To DataFrame β How To Create
Convert lists to DataFrame in Pandas
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 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