Home > Net >  How can I add heading on the columns
How can I add heading on the columns

Time:11-19

How can I add columns to the dataframe xFlattened ? In my case xFlattened.columns didn`t help me.

def corrFilter(x, bound):
    xCorr = x.corr()
    xFiltered = xCorr[((xCorr >= bound) | (xCorr <= -bound)) & (xCorr !=1.000)]
    xFlattened = xFiltered.unstack().sort_values(ascending = False).drop_duplicates()
    xFlattened.columns = ["first_value", "second_value", "corr"]
    return xFlattened

d = {'review_scores_accuracy': [1.1, 2.0, 4.5, 5.0, 4.9, 4.8, 4.7], 
     'review_scores_rating': [1.1, 2.0, 4.6, 3.9, 4.2, 4.5, 4.2]}
df = pd.DataFrame(data=d)

df = corrFilter(df, .7)
df

What I want

             first_value              second_value      percentage
0 review_scores_accuracy      review_scores_rating        0.968217
1                           review_scores_accuracy             NaN

CodePudding user response:

You need to add reset_index in your function to convert the Series to DataFrame:

xFlattened = xFiltered.unstack().sort_values(ascending = False).drop_duplicates().reset_index()

output:

              first_value            second_value      corr
0  review_scores_accuracy    review_scores_rating  0.968217
1  review_scores_accuracy  review_scores_accuracy       NaN
  • Related