我有一个包含2列的DataFrame - 一列带有单词,另一列带有pos_tags。
word1 tag1
0 Why WRB
1 is VBZ
2 this DT
3 happening NN
4 to TO
5 us PRP
6 Asterix NNP
7 and CC
8 Obelix NNP
如何添加另外两列,其值为列'word1'&的第(i + 1)个值'tag1'导致;
word1 tag1 word2 tag2
0 Why WRB is VBZ
1 is VBZ this DT
2 this DT happening NNP
3 happening NNP to TO
4 to TO us PRP
5 us PRP Asterix NNP
6 Asterix NNP and CC
7 and CC Obelix NNP
8 Obelix NNP nan nan
答案 0 :(得分:3)
在数据框上使用shift(-1)
In [109]: df[['word2', 'tag2']] = df.shift(-1)
In [110]: df
Out[110]:
word1 tag1 word2 tag2
0 Why WRB is VBZ
1 is VBZ this DT
2 this DT happening NN
3 happening NN to TO
4 to TO us PRP
5 us PRP Asterix NNP
6 Asterix NNP and CC
7 and CC Obelix NNP
8 Obelix NNP NaN NaN
答案 1 :(得分:2)
通过调用shift
添加新列,并将-1
作为间隔:
In [84]:
df['word2'], df['tag2'] = df['word1'].shift(-1), df['tag1'].shift(-1)
df
Out[84]:
word1 tag1 word2 tag2
0 Why WRB is VBZ
1 is VBZ this DT
2 this DT happening NN
3 happening NN to TO
4 to TO us PRP
5 us PRP Asterix NNP
6 Asterix NNP and CC
7 and CC Obelix NNP
8 Obelix NNP NaN NaN