regex Python -排除循环中的故障,将列值中的字符串与字典键匹配,并将值替换回列

k5hmc34c  于 2023-08-08  发布在  Python
关注(0)|答案(1)|浏览(57)

我有一个包含电话名称和电池的数据框。我需要做的是,如果数据框中的电话名称与iPhone_dict中的键匹配,则将电池列替换为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]
})

字符串
创建了以下代码,但两者都做了相同的事情

### 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


结果是

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


代码检查“iPhone14”的第一次出现,并忽略其余的。
理想情况下,我希望输出为。

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

w9apscun

w9apscun1#

您可以尝试“清理”电话名称-删除颜色,然后使用.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)

字符串
印刷品:

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

相关问题