2

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]

1 Answer 1

4

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.