I have few lists defined as shown below which needs to be converted into a pandas dataframe. Although I have provided just 4 lists, in my problem, I may have n number of lists and I do not have any prior knowledge of names of these lists with the exception of one list col_names
.
list1 = [1,2,3,4]
list2 = [5,6,7,8]
list3 = [9,10,11,12]
.
.
.
listn = [....]
col_names = ['A', 'B', 'C', 'D']
Desired output is a pandas dataframe df
combining all the n lists and the one list col_names
as a column name as shown below:
import pandas as pd
df = pd.Dataframe([list1, list2, list3,.....,listn], columns = col_names)
print(df)
A B C D
0 1 2 3 4
1 5 6 7 8
2 9 10 11 12
. . . . .
. . . . .
n . . . .
I have tried getting a list of all variables using globals()
by referring to inputs from this question. But this method returns only names of variables as strings and stores them in a dictionary. I am unable to check which of those variables are lists so that I can perform some kind of iteration using which I can append all list values to the dataframe df
. Any guidance on how I can solve this would really be appreciated.
I am using Python 3.7.4 on Windows 10 (x64).