Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python – troubleshoot loop to match a string in a column value with a dictionary key and replace the value back to a column

I have a data frame with Phone Name and Battery. What I need to do is to replace the Battery column with the size of the battery that is in iphone_dict if the Phone Name in the data frame matches a key in the iphone_dict.

import re

## Data Dictionary
iphone_dict = {
  "iPhone 14 Pro Max": 4323,
  "iPhone 14 Plus": 4325,
  "iPhone 14 Pro": 3200,
  "iPhone 14": 3279,
  "iPhone 13 Pro Max": 3095,
  "iPhone 13 Pro": 3095,
  "iPhone 13": 3095,
  "iPhone 13 mini": 2406,
  "iPhone 12 Pro Max": 2815,
  "iPhone 12 Pro": 2815,
  "iPhone 12": 2815,
  "iPhone 12 mini": 2406,
  "iPhone SE (2022)": 2018,
  "iPhone SE (2020)": 1821,
  "iPhone 11 Pro Max": 3969,
  "iPhone 11 Pro": 3046,
  "iPhone 11": 3110,
  "iPhone XS Max": 3174,
  "iPhone XS": 2658,
  "iPhone XR": 2716,
  "iPhone X": 2716,
  "iPhone 8 Plus": 2691
}

phone_names = ["iPhone 14 Pro Max (Purple)", "iPhone 14 Plus (Black)", "iPhone 14 Pro (White)", "iPhone 14", "iPhone 13 Pro Max"]

df = pd.DataFrame({
  "Phone Name": phone_names,
  "Battery": [1, 2, 3, 4, 5]
})

Created following code but both did the same

### My first version 
for phone_name, battery in iphone_dict.items():
  pattern = re.compile(phone_name)
  for index, row in df.iterrows():
    if pattern.search(row["Phone Name"]):
      df.loc[index, "Battery"] = battery

### My Second version
for phone_name, battery in iphone_dict.items():
  patterns = [phone_name, phone_name + " ", " " + phone_name]
  for pattern in patterns:
    for index, row in df.iterrows():
      if pattern in row["Phone Name"]:
        df.loc[index, "Battery"] = battery

The result is

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

                   Phone Name  Battery
0  iPhone 14 Pro Max (Purple)     3279
1      iPhone 14 Plus (Black)     3279
2       iPhone 14 Pro (White)     3279
3                   iPhone 14     3279
4           iPhone 13 Pro Max     3095

the code checks for the first occurrence of "iPhone 14" and ignores the rest.

Ideally I want the output to be.

                   Phone Name  Battery
0  iPhone 14 Pro Max (Purple)     4323
1      iPhone 14 Plus (Black)     4325
2       iPhone 14 Pro (White)     2300
3                   iPhone 14     3279
4           iPhone 13 Pro Max     3095

>Solution :

You can try to "clean" the phone names – remove the color and then use .map:

df = pd.DataFrame({'Phone Name': phone_names})
df['Cleaned phone Name'] = df['Phone Name'].str.replace(r'\s*\(\D+\)\s*$', '', regex=True)
df['Battery'] = df['Cleaned phone Name'].map(iphone_dict)

print(df)

Prints:

                   Phone Name Cleaned phone Name  Battery
0  iPhone 14 Pro Max (Purple)  iPhone 14 Pro Max     4323
1      iPhone 14 Plus (Black)     iPhone 14 Plus     4325
2       iPhone 14 Pro (White)      iPhone 14 Pro     3200
3                   iPhone 14          iPhone 14     3279
4           iPhone 13 Pro Max  iPhone 13 Pro Max     3095
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading