Python附加到数据帧中的特定列

Python附加到数据帧中的特定列,python,python-3.x,dataframe,append,Python,Python 3.x,Dataframe,Append,我有一个数据框df,有3列,还有一个循环,根据循环的列名从文本文件创建字符串: exampletext = "Nr1 thisword1 and Nr2 thisword2 and Nr3 thisword3" Columnnames = ("Nr1", "Nr2", "Nr3") df1= pd.DataFrame(columns = Columnnames) for i in range(0,len(Columnnames)): solution = exampletext.find(

我有一个数据框df,有3列,还有一个循环,根据循环的列名从文本文件创建字符串:

exampletext = "Nr1 thisword1 and Nr2 thisword2 and Nr3 thisword3"
Columnnames = ("Nr1", "Nr2", "Nr3")
df1= pd.DataFrame(columns = Columnnames)
for i in range(0,len(Columnnames)):
   solution = exampletext.find(Columnnames[i])
   lsolution= len(Columnnames[i])
   Solutionwords = exampletext[solution+lsolution:solution+lsolution+10]
现在,我想将数据帧df1末尾的solutionwords附加到正确的字段中,例如,在查找Nr1时,我想将solutionwords附加到名为Nr1的列中。 我试着使用append并创建一个列表,但这只会在列表的末尾追加。我需要数据框来根据我要找的单词来分隔单词。谢谢你的帮助

编辑以获得所需的输出和可读性: 所需输出应为数据帧,如下所示:

Nr1 | Nr2 | Nr3


thisword1 | thisword2 | thisword3

我假设单元格值的单词总是跟在列名后面,并用空格分隔。在这种情况下,我可能会尝试将您的值添加到字典中,然后在它包含您想要的数据后从中创建一个dataframe,如下所示:

example_text = "Nr1 thisword1 and Nr2 thisword2 and Nr3 thisword3"
column_names = ("Nr1", "Nr2", "Nr3")

d = dict()
split_text = example_text.split(' ')
for i, text in enumerate(split_text):
    if text in column_names:
        d[text] = split_text[i+1]

df = pd.DataFrame(d, index=[0])
这将给你:

>>> df 
         Nr1        Nr2        Nr3
0  thisword1  thisword2  thisword3

你能为你的示例文本添加所需的输出吗?我已经相应地编辑了这个问题,谢谢。