SMadden
SMadden

Reputation: 49

How to use user input to create a column in pandas

I have a data table:

sample_data = {'Sample': ['A', 'B', 'A', 'B'],
                'Surface': ['Top', 'Bottom', 'Top', 'Bottom'],
                'Intensity' : [21, 32, 14, 45]}

sample_dataframe = pd.DataFrame(data=sample_data)

I would like to add user input to create a column 'Condition' based on the 'Sample' column. The function below returns the error "TypeError: 'DataFrame' object is not callable"

def get_choice(df, column):
    for i in column:
        user_input = input('Condition= ')
        df['Condition'] = df(user_input)
    return df

get_choice(sample_dataframe, 'Sample')

Upvotes: 1

Views: 1158

Answers (1)

Adhun Thalekkara
Adhun Thalekkara

Reputation: 723

i believe you where trying to add inputs corresponding to elements in sample to a new column.

import pandas as pd
sample_data = {'Sample': ['A', 'B', 'A', 'B'],
                'Surface': ['Top', 'Bottom', 'Top', 'Bottom'],
                'Intensity' : [21, 32, 14, 45]}
sample_dataframe = pd.DataFrame(data=sample_data)
def get_choice(df, column):
    user_input=[]
    for i in df[column]:
        print(i)
        user_input.append(input('Condition= '))
    df['Condition'] = user_input
    return df

print(get_choice(sample_dataframe, 'Sample'))

Upvotes: 1

Related Questions