Home > Back-end >  pandas dataframe custom coloumns and rows
pandas dataframe custom coloumns and rows

Time:01-27

so,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 Much coloumns : "))
list1 = []
list2=[]
nilaiRand = R.randint(1,30)
for i in range(0,n):
    print("Coloumn-",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 coloumns | 'list2' is the random number which is the rows

so it need input how many coloumns you want to make,and it will request the name of each coloumns 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)

so,the output should be like

How Much coloumns : 3
Coloumn-1 : Eli
Coloumn-2 :Chick
Coloumn-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.

  •  Tags:  
  • Related