Jürgen K.
Jürgen K.

Reputation: 3487

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!

Upvotes: 0

Views: 58

Answers (2)

Arkadi
Arkadi

Reputation: 198

Slightly shorter than the current proposal

pd.DataFrame(np.ones(10))

Or

pd.DataFrame(10*[1])

Upvotes: 1

ivallesp
ivallesp

Reputation: 2202

Try this!

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

Upvotes: 1

Related Questions