熊猫读取(Excel)单元格,并返回查找的值

时间:2019-08-09 09:23:11

标签: python pandas dataframe

Excel文件中的一列显示了一些描述的简短形式。它们是字典中的一对一关系。

我想分别查找它们,并将查找的值与简短形式并排写入新文件。

xlrd和xlwt是基本的,所以我用了它们:

product_dict = {
"082" : "Specified brand(s)",
"035" : "Well known brand",
"069" : "Brandless ",
"054" : "Good middle class restaurant",
"062" : "Modest class restaurant"}

import xlwt, xlrd

workbook = xlrd.open_workbook("C:\\file.xlsx")
old_sheet = workbook.sheet_by_index(0)

book = xlwt.Workbook(encoding='cp1252', style_compression = 0)
sheet = book.add_sheet('Sheet1', cell_overwrite_ok = True)

for row_index in range(1, old_sheet.nrows):
    new_list = []   
    Cell_a = str(old_sheet.cell(row_index, 0).value)

    for each in Cell_a.split(", "):

        new_list.append(product_dict[each])

    sheet.write(row_index, 0, Cell_a)
    sheet.write(row_index, 1, "; ".join(new_list))

book.save("C:\\files-1.xls")

看起来还可以。但是我想学习熊猫做同样的事情。

除了下面的内容外,熊猫的样子如何?谢谢。

data = {'Code': ["082","069","054"]}
df = pd.DataFrame(data)

enter image description here

2 个答案:

答案 0 :(得分:1)

如果您具有python字典形式的查找字典,则可以执行以下操作:

import pandas as pd

lookup_dict = {'1': 'item_1', '2':'item_2'}

# Create example dataframe
df_to_process = pd.DataFrame()
df_to_process['code'] = ['1, 2', '1', '2']

# Use .apply and lambda function to split 'code' and then do a lookup on each item
df_to_process['code_items'] = df_to_process['code'].apply(lambda x: '; '.join([lookup_dict[code] for code in x.replace(' ','').split(',')]))

以您的示例为例:

import pandas as pd

product_dict = {
"082" : "Specified brand(s)",
"035" : "Well known brand",
"069" : "Brandless ",
"054" : "Good middle class restaurant",
"062" : "Modest class restaurant"}

data = {'Code': ["082","069","054"]}
df = pd.DataFrame(data)

df['code_items'] = df['Code'].apply(lambda x: '; '.join([product_dict[code] for code in x.replace(' ','').split(',')]))

答案 1 :(得分:1)

有了给定的数据,我首先将字典map移至新列,然后将aggregate','.join

final=df.assign(New=df.Code.map(product_dict)).agg(','.join).to_frame().T

          Code                                                New
0  082,069,054  Specified brand(s),Brandless ,Good middle clas...

位置:

print(df.assign(New=df.Code.map(product_dict)))

是:

  Code                           New
0  082            Specified brand(s)
1  069                    Brandless 
2  054  Good middle class restaurant