[英]Create a New Column in a Pandas DataFrame, and Set all Cells to a Default Array
I'm trying to build a DataFrame where one of the columns represents a vector. 我正在尝试建立一个DataFrame,其中的一列代表一个向量。 This is the part of code I'm having trouble with:
这是我遇到麻烦的代码部分:
tweets = pd.DataFrame(train_tweets)
tweets["LangClass"] = "und"
tweets["LangVec"] = pd.Series[[0,0,0,0,0,0,0,0,0,0]]
train_tweets is an incoming DataFrame with only two columns, and I want to add a third and fourth column, LangClass and LangVec. train_tweets是只有两列的传入DataFrame,我想添加第三列和第四列LangClass和LangVec。 The values in LangVec will be updated element by element.
LangVec中的值将逐个元素更新。
I had it working by using a for loop to iterate through the DataFrame and setting each value of LangVec to the desired vector, but that seems to be a very slow approach. 我通过使用for循环来遍历DataFrame并将LangVec的每个值设置为所需的矢量来使其工作,但这似乎是一种非常慢的方法。
Thanks for any suggestions! 感谢您的任何建议!
I think the best is create list of tuples or list of lists and then call DataFrame
contructor: 我认为最好的方法是创建元组列表或列表列表,然后调用
DataFrame
构造DataFrame
:
L = []
for x in iterator:
first_val = some_code_for_count_val
second_val = some_code_for_count_val
L.append((first_val, second_val))
df1 = pd.DataFrame(L, columns = ['LangClass', 'LangVec'])
Last join to original DataFrame: 最后加入原始DataFrame:
df = df.join(df1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.