Export test Data to Excel by Pandas

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def generateTestDataFile(path,testData_list):
df=pd.DataFrame(testData_list)
df_testdata=df[df['TransactionType'].isin(['NewCard Sale','MoneyPak Sale', 'GDC SwipeReload','GDC Unload','GDN SwipeReload'])]
df_pinkey=df_testdata.filter(items=["PartnerName","TransactionType",'Pinkey'])
df_pinkey=df_pinkey.loc[df_pinkey["Pinkey"]!=""]
df_ls=[]
writer = pd.ExcelWriter(path)
df_testdata.to_excel(writer, sheet_name='TestData', index=False)
df_pinkey.to_excel(writer, sheet_name='Pinkey', index=False)
for column in df_testdata:
column_length = max(df_testdata[column].astype(str).map(len).max(), len(column))
col_idx = df_testdata.columns.get_loc(column)
writer.sheets['TestData'].set_column(col_idx, col_idx, column_length)
for column in df_pinkey:
column_length = max(df_pinkey[column].astype(str).map(len).max(), len(column))
col_idx = df_pinkey.columns.get_loc(column)
writer.sheets['Pinkey'].set_column(col_idx, col_idx, column_length)
writer.save()