英文:
How to manage decoded model number
问题
- 在使用pandas改进工作的便捷性时,我们正在进行工作。
- 通过pandas,可以读取、写入和修改基本的Excel信息。
- 我想按照以下顺序生成结果,请告诉我如何操作。
- 我甚至不知道如何管理包含型号号码含义的信息。
- 这只是一个示例,但型号的数量相当大。
型号号码约定
我应该如何管理这些数据?
我尝试与JSON匹配,但没有正确地做到。
在此输入图片描述
读取型号号码数据框
在此输入图片描述
输出数据框
在此输入图片描述
英文:
We are improving the convenience of work by using pandas.
It is possible to read, write, and modify basic Excel information through pandas.
I'd like to make a result in the following order, so please help me with how to do it.
I don't even know how to manage the information that contains the meaning of the model number.
This is a sample, but the number of models is quite large.
- Read the data frame containing the model number in Excel.
- Load data containing each meaning of the model number. (JSON or ???)
- Decode the model number.
- Decoded content is made into a data frame.
- Save it in Excel.
Model number conventions
How should I manage this data?
I tried matching it with JSON, but I couldn't do it properly.
enter image description here
Read model number Dataframe
enter image description here
Output Dataframe
enter image description here
答案1
得分: 1
如果我正确理解您的目标,您可以使用函数来解析型号编号以获取有用的数据。然后,您可以使用 pandas 的 'apply' 方法来添加每一列。
以下是这些函数可能看起来的示例:
def parse_product_id(model_number):
# 可以在 Python 3.10+ 中使用 match 语句替代字典
p_id_dict = {'Q': 'QLED电视', 'N': 'NEO QLED电视'}
if model_number[0] in p_id_dict:
return p_id_dict[model_number[0]]
else:
return '未知'
def parse_tv_region(model_number):
region_dict = {'A': '亚洲', 'E': '欧洲', 'N': '美洲'}
if model_number[1] in region_dict:
return region_dict[model_number[1]]
else:
return '未知'
def parse_screen_size(model_number):
return int(model_number[2:4]) # 转换为整数是可选的,取决于您的用例
# 继续编写更多函数以解析型号编号的其他部分
然后,我们对每个函数在型号编号上使用 'apply' 方法,并将其设置为数据框中的一列:
# 这是我们结果中列名与生成它们的解析函数之间的配对关系
column_functions = {'产品ID': parse_product_id, '地区': parse_tv_region, '尺寸': parse_screen_size}
# 当然,您可以单独应用每个函数,但这使用的代码更少
for col, func in column_functions.items():
df[col] = df['型号编号'].apply(func)
我生成了一个看起来像这样的数据框:
在运行上述代码后,它看起来像这样:
英文:
If I understand your goal correctly, you can use functions to parse the model number into useful data. Then you can use pandas 'apply' to add each column.
Here's a sample of what these functions could look like
def parse_product_id(model_number):
# Can use a match statement instead of dict in python 3.10+
p_id_dict = {'Q': 'QLED TV', 'N': 'NEO QLED TV'}
if model_number[0] in p_id_dict:
return p_id_dict[model_number[0]]
else:
return 'Unknown'
def parse_tv_region(model_number):
region_dict = {'A': 'ASIA', 'E': 'EUROPE', 'N': 'AMERICA'}
if model_number[1] in region_dict:
return region_dict[model_number[1]]
else:
return 'Unknown'
def parse_screen_size(model_number):
return int(model_number[2:4]) # Conversion to integer is optional, and depends on your use case
# Continue to write more functions to parse other parts of the model number
Then, we use the 'apply' method on the model number for each function, and set it to a column in our dataframe
# This is a pairing between the column names in our outcome, to the parsing function that generates them
column_functions = {'PRODUCT ID': parse_product_id, 'REGION': parse_tv_region, 'SIZE': parse_screen_size}
# You could of course apply each function individually, but this uses less code
for col, func in column_functions.items():
df[col] = df['ModelNumber'].apply(func)
I generated a dataframe that looks like this
And after running the above code, it looks like this
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论