如何使用用户输入在 pandas 中创建列

How to use user input to create a column in pandas

我有一个数据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)

我想添加用户输入以基于 'Sample' 列创建一个列 'Condition'。下面的函数 returns 错误“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')

我相信你在尝试将与示例中的元素对应的输入添加到新列中。

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'))