假设我有一个看起来像这样的数据框:
Response 1 2 3 4 SelectedAnswerString
3 dog cat mouse rabbit
2 cap light watch bag `
我可以从标题为“ response”的列中提取数值(例如3),然后根据该数值(即标题为“ 3”的列)选择该列,以提取该行中的字符串/列(即第一行的鼠标)并使用该字符串创建新列?谢谢!
答案 0 :(得分:0)
您可以使用熊猫dataframe.append(your value, ignore_index=True)
,因此ignore_index参数表示不使用索引标签。如果要提取值,可以使用.loc and .iloc
答案 1 :(得分:0)
使用df
作为数据框,您可以首先使用Response
列中的值来标识列位置的位置,然后在该列位置中抓取该字符串以进行插入在新列中:
import pandas as pd
df[SelectedAnswerString] = df[df.iloc[:, df.Response]]
这假定Response
列为数值。但是,您需要调整以下事实:Python开始索引0