Python 遍历dataframe并根据列值执行代码
我有一个如下的数据框架,我想创建一个新的列“Six”,这样这个列的值取决于“Second”、“Third”、“Forth”、“Fifth”列的值 列0是字符串(有语法错误的句子,我正在进行NLP项目) 第1列到第5列实际上是错误的位置和更正的内容。我将有一个代码来更正第0列中的句子 如果value=NaN,则表示在前2个错误之后不再有错误 如何循环遍历每一行和每一列以获得第6列的最终输出 了解需要创建循环…需要帮助才能开始。谢谢Python 遍历dataframe并根据列值执行代码,python,pandas,dataframe,loops,Python,Pandas,Dataframe,Loops,我有一个如下的数据框架,我想创建一个新的列“Six”,这样这个列的值取决于“Second”、“Third”、“Forth”、“Fifth”列的值 列0是字符串(有语法错误的句子,我正在进行NLP项目) 第1列到第5列实际上是错误的位置和更正的内容。我将有一个代码来更正第0列中的句子 如果value=NaN,则表示在前2个错误之后不再有错误 如何循环遍历每一行和每一列以获得第6列的最终输出 了解需要创建循环…需要帮助才能开始。谢谢 Actual Input A 0 1 2 3
Actual Input
A 0 1 2 3 4 5
S
8 If your genetic... 10 10|||the||| 26 26|||will||| NaN NaN NaN
9 However for... 11 11|||,||| 9 10|||specific NaN NaN NaN
Expected output
A 0 1 2 3 4 5 6
S
8 If your genetic... 10 10|||the||| 26 26|||will||| NaN NaN NaN 'Sentence with no error'
9 However for... 11 11|||,||| 9 10|||specific NaN NaN NaN 'Sentence with no error'
您可以设置如下循环: 首先确保索引不包含重复项:
df = df.reset_index(drop=True)
然后循环行和列,最后将答案放入第6列:
for idx in df.index:
for i range(6): # loop through the columns and do what you need to do here
colVal = df.loc[idx,i] #contains value of the column i
# do whatever
df.loc[idx,6] = your_answer # assign the value to column 6
您可以设置如下循环: 首先确保索引不包含重复项:
df = df.reset_index(drop=True)
然后循环行和列,最后将答案放入第6列:
for idx in df.index:
for i range(6): # loop through the columns and do what you need to do here
colVal = df.loc[idx,i] #contains value of the column i
# do whatever
df.loc[idx,6] = your_answer # assign the value to column 6
这看起来像是你在每一列中计算了许多个1,并将结果存储在六列中吗?嗨,不是真的,让我编辑我的数据。这看起来像是你在每一列中计算了许多个1,并将结果存储在六列中吗?嗨,不是真的,让我编辑我的数据