[英]Add values to dataframe column after certain index
I have a dataframe like this: 我有一个这样的数据框:
No Data Sentence
32 xxx yyyy
45 hhh uuuu
. . .
. . .
8726 aaa bbbb
Where the No
column is unordered, now I have x
which is list of sentences and I want to add that list to the Sentence
column after my last index. 在
No
列无序的地方,现在我有x
,这是句子列表,我想将该列表添加到我的最后一个索引之后的Sentence
列中。 So my new dataframe will look like: 因此,我的新数据框将如下所示:
No Data Sentence
32 xxx yyyy
45 hhh uuuu
. . .
. . .
8726 aaa bbbb
NaN NaN x[0]
NaN NaN x[1]
. . .
. . .
NaN NaN x[n]
I know we can directly assign list to column by assign function but it'll assign list value from beginning and I don't even know size of my list. 我知道我们可以直接通过分配功能将列表分配给列,但是它将从头开始分配列表值,我什至不知道列表的大小。 Can someone help me with this?
有人可以帮我弄这个吗?
First turn your list into a dataframe: 首先将您的列表变成一个数据框:
listDF = pd.DataFrame(list, columns='Sentence')
Then use pandas.DataFrame.append 然后使用pandas.DataFrame.append
extended_df = df.appned(listDF)
To test: 去测试:
create df1: 创建df1:
df = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))
print it: 打印:
A B
0 1 2
1 3 4
create df2: 创建df2:
df2 = pd.DataFrame([5, 6], columns=list('B'))
print it: 打印:
B
0 5
1 6
finally: 最后:
df.append(df2)
results: 结果:
A B
0 1.0 2
1 3.0 4
0 NaN 5
1 NaN 6
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.