Advertisement
collinsanele

A user input to data frame converter written in Python 3.4

Nov 28th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. import pandas as pd
  2. ''' A script that converts user input into a dataframe. Writren by Collins'''
  3. num_columns = int(input('Enter number of columns '))
  4.  
  5. values_container = [list() for x in range(num_columns)]
  6.  
  7. name_of_columns = [str(input('Enter name of column' + ' ' + str(x) + ' ')) for x in range(num_columns)]
  8.  
  9. print("!!!NB Enter a value for the first column then press the enter key to enter more values for the first column. Enter 'q' when done with a column to enter the values of the subsequent columns\nNote all columns must have the same length and value inputs must be terminated with 'q'\n")
  10.  
  11. for x in range(num_columns):
  12. while True:
  13. value = input('Enter value ')
  14. if value == 'q':
  15. break
  16. values_container[x].append(value)
  17.  
  18. zipped = zip(name_of_columns, values_container)
  19.  
  20. dic ={ k:v for k,v in zipped}
  21. try:
  22. df = pd.DataFrame(dic)
  23. print(df)
  24. except Exception as e:
  25. print(str(e))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement