Python 基于行/列的值提取字符串
假设我有一个数据帧,看起来像这样:Python 基于行/列的值提取字符串,python,string,Python,String,假设我有一个数据帧,看起来像这样: Response 1 2 3 4 SelectedAnswerString 3 dog cat mouse rabbit 2 cap light watch bag ` 我是否可以从标题为“response”的列中提取数值(例如3),并根据该数值选择列(例如标题为“3”的列),以提取该行/列中的字符串(即鼠标用于
Response 1 2 3 4 SelectedAnswerString
3 dog cat mouse rabbit
2 cap light watch bag `
我是否可以从标题为“response”的列中提取数值(例如3),并根据该数值选择列(例如标题为“3”的列),以提取该行/列中的字符串(即鼠标用于第一行)并使用该字符串创建新列?谢谢 您可以使用Pandas
dataframe.append(您的值,ignore\u index=True)
,因此ignore\u index参数表示不要使用索引标签。如果您想提取值,可以使用.loc和.iloc
使用df
作为数据帧,您可以从使用响应
列中的值来标识列位置的位置开始,并在该列位置抓取该字符串以插入新列:
import pandas as pd
df[SelectedAnswerString] = df[df.iloc[:, df.Response]]
这假定响应
列有一个数值。但是,您需要根据Python开始索引0这一事实进行调整