0

I have a couple of function which filter a dataframe of different customers' features and return the filtered dataframe as below:

stg_1 = strategy_1_filtering(df_1)
stg_2 = strategy_2_filtering(df_2)

Now, I make an adjacency matrix to based on Customer IDs and strategies:

d = {'stg_1':stg_1['Customer_ID'],'stg_2':stg_2['Customer_ID']}
df  = pd.DataFrame({k: dict.fromkeys(v, True) for k, v in d.items()}).fillna(False)

And the result is something like this:

stg_1 stg_2
385986.0 True True
363015.0 True False
411847.0 True True
413369.0 True True
401081.0 True False

I have tried to change the name of first column using this code:

 df.columns.values[0] = "Customer_ID"

But it did not work. To understand the problem I printed shape and column names of the dataframe:

print(df.shape)
print(df.columns)

The results:

(1155, 2)
Index(['stg_1', 'stg_2'], dtype='object')

In this case, when the dataframe cannot detect even the first column, what should I do?

V.Nouri
  • 207
  • 3
  • 15

2 Answers2

2

Your first column is actually an index. Use rename_axis:

df = df.rename_axis('Customer ID')

If you want the index as column, use reset_index:

df = df.rename_axis('Customer ID').reset_index()
mozway
  • 194,879
  • 13
  • 39
  • 75
1

I assume your first column is actually the index of the DataFrame. To name it:

df.index.name = "Customer_ID"
nikeros
  • 3,302
  • 2
  • 10
  • 26