Reputation: 838
The two data structures I am working with are a dataframe and a list. I need to print rows from the cluster column that match my list at index 0, 1, n... I then need to take user input and feed it to new column.
sample_data = {'Cluster': ['A', 'B', 'A', 'B'],
'column2' : [21, 32, 14, 45]}
sample_dataframe = pd.DataFrame(data=sample_data)
lst = ['A', 'B']
Desired Dataframe after taking user input(the y and n is what the user will input):
expected_output = {'cluster': ['A', 'B', 'A', 'B'],
'column2': ['Top', 'Bottom', 'Top', 'Bottom'],
'NEW_COLUMN' : ['y', 'n', 'y', 'n']}
expected_output = pd.DataFrame(data=expected_output)
I was thinking some sort of loop like:
for i in lst:
if i == column value:
print all rows that match
and take user input to form a new column
I haven't been able to put together the logic for this yet.
Any help would be greatly appreciated! The user input should
for the new column should be put into every row that matches
the list. For example, the new dataframe has user input 'y'
where the Cluster is 'A' and user input 'n' where the cluster is 'B'.
Upvotes: 0
Views: 702
Reputation: 364
I think that should work.
sample_data = {'Cluster': ['A', 'B', 'A', 'B'],
'column2': [21, 32, 14, 45]}
sample_dataframe = pd.DataFrame(data=sample_data)
lst = ['A', 'B']
#Create a dictionary to save the user input
input_dict = {}
for i in lst:
#Genrate Data with right Cluster
series = sample_dataframe[sample_dataframe['Cluster'] == i]
#Check if Cluster from list is in Datafrmae
if series.empty:
continue
else:
print(series)
#Get user input and store it in dictionary
input_dict[i] = input()
#Create new Column
sample_dataframe['input'] = sample_dataframe['Cluster'].map(input_dict)
print(sample_dataframe)
For better unserstanding a dictionary is a list of key/value pair. In this case the Cluster is the key and the user input is the value. To generate the new Column i just map the Cluster column with the dictionary. Maybe a closer look to the map method could help. I hope by and large the code can be understood well. If not please ask for clarification.
Upvotes: 1