2

I have a pandas dataframe of 45 variables of varying data types and I am using 'dython.nominal' package to create a matrix of associations between each of those variables.

I then want to:

A: subset my dataframe (filter by geographical location) and compute an association matrix on that subset, then

B: create and add to second pandas dataframe with the columns of a target variable from the dataframe created in step A.

The resulting dataframe will then be a matrix of correlations with column indices being georgraphical location and row indices being the other 44 variables.

The code I have so far is:

import pandas as pd
from dython.nominal import  compute_associations

temp_df = pd.DataFrame
for item in postcode_partials:
    temp_data = all_data[all_data['Post Code'].str.contains(item)]
    temp_matrix = compute_associations(temp_data, nominal_columns='auto', mark_columns=True, theil_u=True)
    temp_df[item] = temp_matrix['Last Balance (con)'].values

I keep getting the following error:

'type' object does not support item assignment

I'm quite new to pandas as I haven't had much experience of using them in practice. Any help would be gratefully received.

Thanks Andy

Gonçalo Peres
  • 11,752
  • 3
  • 54
  • 83
furbaw
  • 109
  • 1
  • 12
  • what's there in `postcode_partials`? also, Initialization should be `temp_df = pd.DataFrame()`, a call. – Vishnudev Krishnadas Oct 26 '20 at 10:15
  • Hi there. It is a python list of string values (segment of postcode indicating geographical location). It looks like you have answered my question anyway. The problem looks to have been that I didn't include the brackeds after 'Dataframe'. Schoolboy error. :) Thank you for your help, Andy – furbaw Oct 26 '20 at 10:31
  • Nice. Glad to have helped @furbaw – Vishnudev Krishnadas Oct 26 '20 at 10:32

1 Answers1

1

That is not the correct way to create a new instance of the class pandas.DataFrame. Instead of temp_df = pd.DataFrame, for one to create a new instance of that class and assign the object to the local variable temp_df do the following

temp_df = pd.DataFrame()

It works this way, because

Class instantiation uses function notation. Just pretend that the class object is a parameterless function that returns a new instance of the class. [Source]

Additional relevant links:

Gonçalo Peres
  • 11,752
  • 3
  • 54
  • 83