Home > Net >  How to recode/ map shared columns for dataframe stored in a dictionary?
How to recode/ map shared columns for dataframe stored in a dictionary?

Time:05-18

I want to recode the 'Flavor' field, which both data sets share.

I successfully stored the data as data frames in a dictionary, but the names assigned (for ex. 'df_Mike') are strings and not callable/ alterable objects.

Do let me know where I'm going wrong and explain why.

name = ['Mike', 'Sue']

d = {}

for n in name:
    url = f'https://raw.githubusercontent.com/steflangehennig/info4120/main/data/{n}.csv'
    response = urllib.request.urlopen(url)
    d[n] = pd.DataFrame(pd.read_csv(response))

flavor = {1: 'Choclate', 2: 'Vanilla', 3:'Mixed'}

for df in d:
    df.map({'Flavor': flavor}, inplace = True)

Error code:

      1 flavor = {1: 'Choclate', 2: 'Vanilla', 3:'Mixed'}
      3 for df in d:
----> 4     df.map({'Flavor': flavor}, inplace = True)

AttributeError: 'str' object has no attribute 'map'

CodePudding user response:

You are iterating over the keys in the dictionary. If you want to iterate over the values, you can use dict.values(). For example:

for df in d.values():
    df.map({'Flavor': flavor}, inplace = True)

CodePudding user response:

It would be better to iterate over the dict items. So, use

for name, df in d.items():

CodePudding user response:

You can try

for n in name:
    url = f'https://raw.githubusercontent.com/steflangehennig/info4120/main/data/{n}.csv'
    df = pd.read_csv(url)
    d[n] = df

for df in d.values():
    df['Flavor'] = df['Flavor'].map(flavor)

or only in the first loop

for n in name:
    url = f'https://raw.githubusercontent.com/steflangehennig/info4120/main/data/{n}.csv'
    df = pd.read_csv(url)
    df['Flavor'] = df['Flavor'].map(flavor)
    d[n] = df
  • Related