I have been stuck here I don't know why but this is the code:
import pandas as pd
import random as R
n = int(input("How many columns : "))
list1 = []
list2=[]
nilaiRand = R.randint(1,30)
for i in range(0,n):
print("Column-",i 1 ,sep="",end=": ")
kolom = input()
list1.append(kolom)
print(list1)
ind = int(input("Total random number : "))
inde = range(1,ind 1)
list2.append(nilaiRand)
print(list2)
dataframe = pd.DataFrame(list1,list2)
'list1' is the name of the columns, 'list2' is the random number which is the rows.
I need to input how many columns you want to make,and it will request the name of each columns and listed in 'list1'. and I want to make the rows as much as ind = int(input("Total random number : ")) and the fill of the line is random number nilaiRand = R.randint(1,30)
The output should be like
How many columns : 3
Column-1 : Eli
Column-2 :Chick
Column-3 :You
Total random number : 4
Result
Eli Chick You
1 12 22 3
2 21 12 11
3 4 11 21
4 13 14 5
Any solution?
CodePudding user response:
This should do the trick:
import pandas as pd
import random as R
n = int(input("How much columns: "))
dict1 = {}
for i in range(0,n):
column = input()
dict1[column] = []
ind = int(input("Number of rows: "))
for column in dict1.keys():
for _ in range(ind):
dict1[column].append(R.randint(1,30))
dataframe = pd.DataFrame(dict1)
Input:
- 5
- A
- B
- C
- D
- E
- 8
Output:
A B C D E
0 21 1 6 1 1
1 21 18 17 3 5
2 25 3 6 17 23
3 5 8 16 25 23
4 6 16 22 30 27
5 25 17 14 11 13
6 13 26 30 24 11
7 4 21 29 14 2
However, please up your question-writing game. Do not upload images of code/errors when asking a question. Also give a clear desired output and tell us at what point in your code you stumble unto problems.
Update
If you want to start your index at 1, try:
dataframe = pd.DataFrame(dict1, index=range(1, ind 1))
