Adding column with filter in Pandas

Asked

Viewed 399 times

1

I have a csv with three columns:

+----------------------------+-----------------+--------------+
| txtDescricaoEspecificacao  |  txtFornecedor  |  vlrLiquido  |
+----------------------------+-----------------+--------------+

I wanted him to add the vlrLiquido whenever the txtDescricaoEspecificacao == "CASCOL COMBUSTIVEIS PARA VEICULOS LTDA" and join in a Dataframe showing the txtFornecedor along with the vlrLiquido summed up, someone can help me?

1 answer

0

The first step is to filter the Dataframe where vlrLiquido is "CASCOL COMBUSTIVEIS PARA VEICULOS LTDA": df[df['txtDescricaoEspecificacao'] == 'CASCOL COMBUSTIVEIS PARA VEICULOS LTDA']

Then we take this filtered Dataframe and group the same data from the column txtFornecedor with the addition rule for the column vlrLiquido: .groupby(['txtFornecedor']).agg({'vlrLiquido':sum}).

All together:

df[df['txtDescricaoEspecificacao'] == 'a'].groupby(['txtFornecedor']).agg({'vlrLiquido':sum})

Browser other questions tagged

You are not signed in. Login or sign up in order to post.