Home > Mobile >  How to create two variables columns
How to create two variables columns

Time:12-21

I try to recreate this :

Text

The main problem is that i have 160 users and 26 pages. And i need to add or remove ID or page ID sometimes. How can i do that with Python ?

I see two separate problems, first to create it in Python and then to change it to "columns" like in Excel or CSV and i don't know where to start ?

Expect that :

users = ["a", "b", "c"]
liste_page = []

for i in range(27):
    liste_page.append(i)

full = dict(zip(liste_page, users))

Thanks for your help

CodePudding user response:

This should work for you I believe:

import pandas as pd
import itertools


a=tuple(map(lambda x:f'User {x}' ,list(range(1,161))))
b=range(1,27)

final_df = pd.DataFrame(list(itertools.product(a, b)), columns = ['ID', 'Page ID'])

final_df.to_csv('Final_output.csv', index=False)

Final output is:

print(final_df)

            ID  Page ID
0       User 1        1
1       User 1        2
2       User 1        3
3       User 1        4
4       User 1        5
...        ...      ...
4155  User 160       22
4156  User 160       23
4157  User 160       24
4158  User 160       25
4159  User 160       26

[4160 rows x 2 columns]
  • Related