We Are Going To Discuss About Take multiple lists into dataframe. So lets Start this Python Article.
Take multiple lists into dataframe
I think you’re almost there, try removing the extra square brackets around the lst
‘s (Also you don’t need to specify the column names when you’re creating a dataframe from a dict like this):import pandas as pd lst1 = range(100) lst2 = range(100) lst3 = range(100) percentile_list = pd.DataFrame( {'lst1Title': lst1, 'lst2Title': lst2, 'lst3Title': lst3 }) percentile_list lst1Title lst2Title lst3Title 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 ...
If you need a more performant solution you can use np.column_stack
rather than zip
as in your first attempt, this has around a 2x speedup on the example here, however comes at bit of a cost of readability in my opinion:import numpy as np percentile_list = pd.DataFrame(np.column_stack([lst1, lst2, lst3]), columns=['lst1Title', 'lst2Title', 'lst3Title'])
I think you’re almost there, try removing the extra square brackets around the lst
‘s (Also you don’t need to specify the column names when you’re creating a dataframe from a dict like this):import pandas as pd lst1 = range(100) lst2 = range(100) lst3 = range(100) percentile_list = pd.DataFrame( {'lst1Title': lst1, 'lst2Title': lst2, 'lst3Title': lst3 }) percentile_list lst1Title lst2Title lst3Title 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 ...
If you need a more performant solution you can use np.column_stack
rather than zip
as in your first attempt, this has around a 2x speedup on the example here, however comes at bit of a cost of readability in my opinion:import numpy as np percentile_list = pd.DataFrame(np.column_stack([lst1, lst2, lst3]), columns=['lst1Title', 'lst2Title', 'lst3Title'])
Solution 1
I think you’re almost there, try removing the extra square brackets around the lst
‘s (Also you don’t need to specify the column names when you’re creating a dataframe from a dict like this):
import pandas as pd
lst1 = range(100)
lst2 = range(100)
lst3 = range(100)
percentile_list = pd.DataFrame(
{'lst1Title': lst1,
'lst2Title': lst2,
'lst3Title': lst3
})
percentile_list
lst1Title lst2Title lst3Title
0 0 0 0
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
6 6 6 6
...
If you need a more performant solution you can use np.column_stack
rather than zip
as in your first attempt, this has around a 2x speedup on the example here, however comes at bit of a cost of readability in my opinion:
import numpy as np
percentile_list = pd.DataFrame(np.column_stack([lst1, lst2, lst3]),
columns=['lst1Title', 'lst2Title', 'lst3Title'])
Original Author maxymoo Of This Content
Solution 2
Adding to Aditya Guru‘s answer here. There is no need of using map. You can do it simply by:
pd.DataFrame(list(zip(lst1, lst2, lst3)))
This will set the column’s names as 0,1,2. To set your own column names, you can pass the keyword argument columns
to the method above.
pd.DataFrame(list(zip(lst1, lst2, lst3)),
columns=['lst1_title','lst2_title', 'lst3_title'])
Original Author Abhinav Gupta Of This Content
Solution 3
Adding one more scalable solution.
lists = [lst1, lst2, lst3, lst4]
df = pd.concat([pd.Series(x) for x in lists], axis=1)
Original Author oopsi Of This Content
Solution 4
Just adding that using the first approach it can be done as –
pd.DataFrame(list(map(list, zip(lst1,lst2,lst3))))
Original Author Aditya Guru Of This Content
Conclusion
So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.