Python Forum
Filter value from DataFrame apply a function and save to xlsx
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Filter value from DataFrame apply a function and save to xlsx
#1
Hi
After read my df I need filter to get specific values then use a function, add a column in this new df then save to excel file.
import pandas as pd
colunas = ['Tipo_Lanc','UF','Fis/Jur','CNPJ','RAZAO_SOCIAL','NF','Data_Emissão','DT_Fiscal',
           'Data_Lançamento','PRODUTO','DESCRICAO','NUM_ITEM','Unid','LISTA','NCM','Monit/Liber',
           'EAN','MVA_Original','MVA','CFOP','CST','QUANTIDADE','PF_UNIT','PF_TOTAL','VLR_LIQ_UNIT',
           'VLR_LIQ_ITEM','VL_UNIT_NF','TOTAL_NF','VL_UNIT_LIQ_NF','TOTAL_LIQ_NF','DESC._TOT','DESCONTO',
           'REPASSE','VC','BC','ICMS','BC_Não_Escriturado','ICMS_Não_Escriturado','ALIQ_ICMS','APROPRIA',
           'BC_ICMS_ST','ICMS_ST','ALIQ_INTERNA','DEB_ICMS','IPI','CAT_ANVISA','TIPO_PRODUTO','TIPO_DESCONTO',
           '%_DESCONTO','PMC','PMC_FCIA_POP','%_REDUTOR_ANVISA','CROSS','INDICADOR_ICMSS','BC_ICMS_ST_REC',
           'ICMS_ST_REC','MCANCER','CBASICA']
df = pd.read_excel('fat.xlsx', sheet_name='Geral')
df.columns=colunas

# After read df, I need get this two peace of DataFrame
cfop_5403 = df[df['CFOP'] == 5403]
cfop_5102 = df[(df['CFOP'] == 5102)]

# But I need use this function before save to excel file
def substi_trib(x):

    if x['Monit/Liber'] == 'M':
        if (x['CAT_ANVISA'] == "Generico") | (x['CAT_ANVISA'] == "Similar"):
            return x['PMC']*x['QUANTIDADE']*0,50*0,17-x['ICMS']
        elif (x['CAT_ANVISA'] == "Outros") | (x['CAT_ANVISA'] == ""):
            return x['PMC']*x['QUANTIDADE']*0,90*0,17-x['ICMS']
        elif (x['CAT_ANVISA'] == "Referencia"):
             return x['PMC']*x['QUANTIDADE']*0,88*0,17-x['ICMS']
        else:
            return 0

    if x['Monit/Liber'] == 'L':
        return 1 + x['MVA'] * x['TOTAL_LIQ_NF'] * 0,17 - x['ICMS']
    
# How can I add acolumn and write a function result in this column, after save as sheet into excel file?
cfop_5403['col_test'] = cfop_5403.apply(substi_trib, axis=1)


# I would like write result in excel. 
with pd.ExcelWriter('teste.xlsx', engine='xlsxwriter') as ex:
    cfop_5403.to_excel(ex, sheet_name='CFOP_5403', index=False)
    cfop_5102.to_excel(ex, sheet_name='CFOP_5102', index=False)
Thank you
Reply
#2
I solved this.
I put my filter after function
Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question How can I save cartesian products to a dataframe? noahverner1995 1 1,636 Dec-27-2021, 09:15 AM
Last Post: noahverner1995
  apply(pd.Series) until no more array mikisDeWitte 1 2,774 Apr-17-2021, 08:45 PM
Last Post: Caprone
  Export dataframe to xlsx - Error "zipfile.BadZipFile: File is not a zip file" Baggio 10 62,118 Mar-12-2021, 01:02 PM
Last Post: buran
  Outputs "NaN" after "DataFrame columns" function? epsilon 7 3,658 Jan-27-2021, 10:59 AM
Last Post: epsilon
  How to Filter a Dataframe mfkzolo 4 2,315 Nov-20-2020, 02:57 PM
Last Post: sandeep_ganga
  Filter data based on a value from another dataframe column and create a file using lo pawanmtm 1 4,282 Jul-15-2020, 06:20 PM
Last Post: pawanmtm
  Apply rolling window function over time dimension of 3D data Staph 0 2,185 Jan-01-2020, 08:31 AM
Last Post: Staph
  Apply function on different columns as defined DavidGG 6 5,002 Sep-04-2019, 03:35 PM
Last Post: vivekirti
  how to apply user defined function to Pandas DataFrame evelynow 3 7,621 Aug-20-2019, 11:35 PM
Last Post: scidam
  Apply Method Question smw10c 4 5,549 Apr-08-2017, 12:47 PM
Last Post: smw10c

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020