All Courses

Create a dataframe with number of column and and column names according to the user

By Anusingh3211, 5 months ago
  • Bookmark

Can you please help me with this how to do it using by getting things from user because in session we have learnt where we already know the number of columns and name of them so we created list as per them but how to do this when user say suppose create 5 column. How to make 5 list ?

1 Answer

You can refer the first recording of the ml class , the problem is solved

import pandas as pd

number_of_columns = int(input("enter ther number of columns -->"))

columns = []

for i in range(number_of_columns):

  columns.append(input("enter the column name-->"))

dct = {}

number_of_values_in_each_col = int(input("enter the number of values in each column-->"))

for i in range(number_of_columns):

  lst = []

  for j in range(number_of_values_in_each_col):

    lst.append(input(f"enter the value for column {columns[i]}--->"))

  dct[columns[i]] = lst

Data = pd.DataFrame(dct)


Your Answer


Why You Should Learn Data Science in 2022?

Dec 15th (7:00 PM) 277 Registered
More webinars

Related Discussions

Running random forest algorithm with one variable

View More