Assign numpy array to pandas mask

Viewed 541

I performed a task on a pandas masked subset:

pdxy = pd.DataFrame(data,columns=['X','Y','C','CC'])
mask = pdxy[:]['Y']==8

print("pdxy[mask]")
print(pdxy[mask][:10])

pdxy[mask]
       X  Y  C  CC
17    17  8  0   0
18    18  8  0   0
48    48  8  0   0
56    56  8  0   0
63    63  8  0   0
66    66  8  0   0
73    73  8  0   0
87    87  8  0   0
103  103  8  0   0
116  116  8  0   0

kmeans = KMeans(n_clusters=5,random_state=0).fit(pdxy[mask]['X','Y'])

afterwards I want to assing the results (clusters and cluster centers) to columns in the pandas dataframe:

pdxy.loc[mask]['C']  = np.array(kmeans.labels_)
pdxy.loc[mask]['CC'] = np.array(kmeans.cluster_centers_[kmeans.labels_])[:,0]

Unfortunately the DataFrame is not modified, i.e. as before the assignement:

print("pdxy[mask] labeled")
print(pdxy[mask][:10]) 

pdxy[mask] labeled
       X  Y  C  CC
17    17  8  0   0
18    18  8  0   0
48    48  8  0   0
56    56  8  0   0
63    63  8  0   0
66    66  8  0   0
73    73  8  0   0
87    87  8  0   0
103  103  8  0   0
116  116  8  0   0

What can I do?

1 Answers

accessing a row+column with .loc is done with comma, as [row, col] and not [row][col]

try this:

import numpy as np
import pandas as pd

pdxy = pd.DataFrame(data, columns=['X', 'Y', 'C', 'CC'])
mask = pdxy[:]['Y'] == 8

kmeans = KMeans(n_clusters=5,random_state=0).fit(pdxy[mask]['X','Y'])

pdxy.loc[mask, 'C']  = np.array(kmeans.labels_)
pdxy.loc[mask, 'CC'] = np.array(kmeans.cluster_centers_[kmeans.labels_])[:,0]

print("pdxy[mask] labeled")
print(pdxy[mask][:10]) 
Related