How to Convert a List to a pandas DataFrame
In this tutorial you will convert a Python list to a pandas DataFrame.
TLDR solution
import pandas as pd
python_list = ['element1', 'element2', 'element3']
df = pd.DataFrame(python_list, columns=['column_name'])
Example 1: Convert a Simple List
Let's say, you want to convert the following list:
fishes = ['salmon', 'pufferfish', 'shark']
You can then use pandas to convert it to a DataFrame as follows:
import pandas as pd
df = pd.DataFrame(fishes, columns=['fish_name'])
Verify by printing the DataFrame:
print(df)
The output looks like this:
fish_name
0 salmon
1 pufferfish
2 shark
Example 2: Convert a List of Lists
Let's say, you want to convert the following list of lists:
fishes_caught = [['salmon', 5], ['pufferfish', 1], ['shark', 0]]
To convert it into a DataFrame, use the following code:
import pandas as pd
df = pd.DataFrame(fishes_caught, columns=['fish_name', 'count'])
That's it! You just learned how to convert a Python list into a pandas DataFrame.