Alicia_2024
Alicia_2024

Reputation: 531

How to append array as column to the original Pandas dataframe

I have a data frame that looks like below:

x_1 x_2 x_3 x_combined
0   1    0  [0,1,0]
1   0    1  [1,0,1]
1   1    0. [1,1,0]
0   0    1  [0,0,1]

Then I calculated the centroid of each dimension by using np.mean(df['x_combined'], axis=0) and got

array([0.5, 0.5, 0.5])

How do I now append it back to the original DF as a fifth column and have the same value for each row? It should look like this:

x_1 x_2 x_3  x_combined  centroid
0   1    0  [0,1,0]     [0.5, 0.5, 0.5]
1   0    1  [1,0,1]     [0.5, 0.5, 0.5]
1   1    0. [1,1,0]     [0.5, 0.5, 0.5]
0   0    1  [0,0,1]     [0.5, 0.5, 0.5]

Upvotes: 2

Views: 344

Answers (1)

Bill
Bill

Reputation: 11613

This also works:

df = pd.DataFrame({
    'x_1': [0, 1, 1, 0],
    'x_2': [1, 0, 1, 0],
    'x_3': [0, 1, 0, 1],
    'x_combined': [np.array([0, 1, 0]), np.array([1, 0, 1]),
                   np.array([1, 1, 0]), np.array([0, 0, 1])]
})

a = np.mean(df['x_combined'], axis=0)  # or a = df['x_combined'].mean(axis=0)
df['centroid'] = [a]*len(df)

Upvotes: 4

Related Questions