简体   繁体   English

在Pandas DataFrame中创建一个新列,并将所有单元格设置为默认数组

[英]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.

相关问题 python pandas dataframe从其他列的单元格创建新列 - python pandas dataframe create new column from other columns' cells pandas Dataframe 创建新列 - pandas Dataframe create new column 在熊猫数据框(每个单元格中都有一个集合)中,当将新元素添加到特定单元格中的集合时,元素会添加到所有单元格中 - In pandas dataframe (with a set in every cell) when adding new element to a set in a specific cell, element is added to all cells 将列和默认数据追加到新的Pandas DataFrame中 - Append column and default data into new Pandas DataFrame 如何从另一列的所有值创建新的列名并按 pandas dataframe 中的另一列创建新列名? - how to create new column names from another column all values and agg by another column in pandas dataframe? Pandas 通过从另一个数据帧的 1 列中的单元格检查列表中返回匹配字符串的行来创建新的数据帧 - Pandas create a new dataframe by returning the rows matching strings from a list checked against cells in 1 column from an another dataframe 从日期时间设置新列 dataframe pandas - Set new column from datetime on dataframe pandas 创建一个新列作为 Pandas DataFrame 的计数 - Create a new column as a count of the Pandas DataFrame 创建带有一些逻辑的新列到 Pandas 数据框 - Create new column with some logic to pandas dataframe 循环将创建新的Pandas.DataFrame列 - Loop that will create new Pandas.DataFrame column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM