'How to recode/ map shared columns for dataframe stored in a dictionary?
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'
Solution 1:[1]
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)
Solution 2:[2]
It would be better to iterate over the dict items. So, use
for name, df in d.items():
Solution 3:[3]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Josh Clark |
| Solution 2 | nutcracker |
| Solution 3 | Ynjxsjmh |
