简体   繁体   English

在熊猫数据框中插入numpy列

[英]Insert numpy column in pandas data frame

I need a data frame, which contains just one column which is filled with ones. 我需要一个数据框,其中仅包含一列,其中填充有一列。 Here is my code so far. 到目前为止,这是我的代码。

label_values = np.empty(10)
    label_values.fill(1)
    label_df = pd.DataFrame()
    label_df.insert(column=0, value=label_values.transpose(), allow_duplicates=True)

I get the following error: 我收到以下错误:

    label_df.insert(column=0, value=label_values.transpose(), allow_duplicates=True)
TypeError: insert() missing 1 required positional argument: 'loc'

As far as I see the problem is that the position (row) for the insert is missing. 据我所知,问题是插入的位置(行)丢失了。 Do I really need to iterate over the array and insert each single value? 我真的需要遍历数组并插入每个值吗?

Thanks in advance! 提前致谢!

Try this! 尝试这个!

label_values = np.empty(10)
label_values.fill(1)
label_df = pd.DataFrame()
label_df[0] = label_values

Slightly shorter than the current proposal 比目前的提案略短

pd.DataFrame(np.ones(10))

Or 要么

pd.DataFrame(10*[1])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM