How to remove dtype from dataframe line?

Asked

Viewed 47 times

1

Running the code:

def novacoluna(df):
  coluna_adicionada = {}
  coluna_adicionada['retorno_diario']= df['quota_value']/df['quota_deslocada']
  return pd.Series(coluna_adicionada, index=['retorno_diario'])

agrupamento_por_fundo = df.groupby([df['fund_name'], df['date']]).apply(novacoluna)

To add a new column to the grouped dataframe, when I run group_por_background.head() the data appears this way:

inserir a descrição da imagem aqui

How do I add only account results in column, without 10397 before and dtype after?

  • 1

    pq Have you created a function to add column? Try to create the column directly

2 answers

1


Creating a column as a result of two others is simpler than it looks

Create the Dataframe

import pandas as pd

>>> df = pd.DataFrame({'quota_value': [10.10, 20.0, 15.50, 50.0], 'quota_deslocada': [2, 2, 5, 5]})

>>> df
   quota_value  quota_deslocada
0         10.1                2
1         20.0                2
2         15.5                5
3         50.0                5

Create the new column based on the other two

>>> df['retorno_diario'] = df['quota_value']/df['quota_deslocada']

>>> df
   quota_value  quota_deslocada  retorno_diario
0         10.1                2            5.05
1         20.0                2           10.00
2         15.5                5            3.10
3         50.0                5           10.00

I hope it helps

1

The problem is that your function creates a series with the data you need, and then you create a dataframe containing the series, in the line that has the "Return". In the answer the content of each cell is itself a complete series, and the series representation includes the information of 'dtype'.

Your function could be just:

def novacoluna(df):
  
  coluna_adicionada = df['quota_value']/df['quota_deslocada']
  coluna_adicionada.name = 'retorno_diario'
  return coluna_adicionada
  

Browser other questions tagged

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