-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_input.py
More file actions
57 lines (42 loc) · 1.83 KB
/
data_input.py
File metadata and controls
57 lines (42 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import pandas a?s pd
import pickle
data = pd.read_pickle('dataframe.pkl')
# IF YOU WANT TO EXTEND THE SAVED DATAFRAMEINPUT USE THIS INSTEAD:
#data = pd.read_pickle('dataframeInput.pkl')
print(data)
print(data.dtypes)
# Suppose 'data' is your DataFrame and arr_col_names is a list of column names in 'data'
input_cols = data.drop(['Station abbreviation'], axis =1)
arr_col_names = list(input_cols.columns)
# Initialize new_row_dict with string indices and NA values
new_row_dict = {col_name: pd.NA for col_name in arr_col_names}
def convert_input_to_type(user_input, wanted_type):
try:
return wanted_type.type(user_input)
except ValueError:
raise ValueError(f"Wrong datatype. Please input a {wanted_type}.")
def input_in_dict(arr_col_name, input_func=input):
wanted_type = data[arr_col_name].dtype
print(f"Expecting type {wanted_type} for column '{arr_col_name}'")
while True:
user_input = input_func(f"Input {arr_col_name}: ")
try:
converted_input = convert_input_to_type(user_input, wanted_type)
new_row_dict[arr_col_name] = converted_input
break # Break out of the loop if the conversion succeeds
except ValueError as e:
print(e)
print(f"Error occurred for column '{arr_col_name}' with input '{user_input}'")
# Iterate over each column name in arr_col_names and prompt for user input
while True:
for col_name in arr_col_names:
input_in_dict(col_name)
# Append the dictionary to the DataFrame
data = data._append(new_row_dict, ignore_index=True)
# Ask the user if they want to input another row
more_rows = input("Do you want to input another row? (y/n): ")
if more_rows.lower() != 'y':
break
print(data)
print("Thanks for your input, it has been saved.")
data.to_pickle('dataframeInput.pkl')