0

I am trying to run a while loop to go through several functions until the final difference = 0 (k-clustering). Here is my current while loop with some print debugging statements included:

diff=1

while (diff!=0):
  cents_old=cents_new[:]
  print(cents_old)
  print(cents_new)
  feat_list=df_features.values.tolist()
  print(feat_list)
  dist_cent1=[]
  dist_cent2=[]
  dist_cent3=[]
  print(dist_cent1)
  dist_to_cent(cents_old, feat_list)
  print(dist_cent1)
  min_index_list=[]
  print(min_index_list)
  min_assign(dist_cent1,dist_cent2,dist_cent3)
  print(min_index_list)
  df_features1=df_features[:]
  df_features1['Closest Centroid']=min_index_list
  print(df_features)
  print(df_features1)
  avg_list_cent1=[]
  avg_list_cent2=[]
  avg_list_cent3=[]
  print(avg_list_cent1)

  
  



  diff=0

As you may be able to tell, I'm still building the loop, just bit by bit.

I am guessing that these print statements are triggering the warning:

print(df_features)
print(df_features1)

Here is the code indexing the two dataframes:

df_features1=df_features[:]

This is the warning:

/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:21: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

I tried reading the article in the pandas notation relating to this, but it seems to apply to columns and subcolumns.

Is this warning simply saying I should more specifically slice my dataframe so that:

df_features1=df_features['col1', 'col2',....]

Thanks for the help.

Veneratu
  • 19
  • 6
  • 1
    `cents_old=cents_new[:]` and `df_features1=df_features[:]` are not safe ways to create copies of Pandas DataFrames. You should use [`copy()`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.copy.html) -> `df_features1=df_features.copy()` – Henry Ecker Oct 11 '21 at 18:00

0 Answers0