import pandas as pd
import easygui as g
source_file = g.fileopenbox(msg=None, title='Select Source', default="*.xlsx", filetypes=None, multiple=False)
df_source = pd.read_excel(source_file, sheet_name=0)
source_dict = {}
cols = ['Survived', 'Pclass', 'Name', 'Sex', 'Age', 'SibSp', 'Parch', 'Ticket', 'Fare', 'Embarked']
df_list = df_source[cols]
for index, row in df_list.iterrows():
key = (row['Survived'], row['Pclass'], row['Name'])
value = [row['Sex'], row['Age'], row['SibSp']]
source_dict[key] = value
print(source_dict)
# 参照下面的代码,读取新的文件,创建新的 df, 根据字典更新对应的行,在保存 excel
# key = row['类型']
# elements = row['含有元素'].split(',')
# for elem in elements:
# result[elem] = result.get(elem, list())
# result[elem].append(fruit)
# # intialise data of lists.
# data = {'元素':[], '水果':[]}
# for key, value in result.items():
# data['元素'].append(key)
# data['水果'].append(','.join(value))
# new_df = pd.DataFrame(data)
# writer = pd.ExcelWriter(excel_file, engine='xlsxwriter')
# df.to_excel(writer, sheet_name='Sheet1', index=False)
# new_df.to_excel(writer, sheet_name='Sheet2', index=False)
# writer.save()
|