1

I'm trying to add after the Gross profit line in an income statement new line with some values from array. I tried just to append it in the location but nothing changed.

income_statement.loc[["Gross Profit"]].append(gross)

The only way i succeed doing something similar is by making it another dataframe and concat it to end of the income_statement.

I'm trying to make it look like that:(The 'gross' line in yellow) enter image description here How can i do it?

1 Answer 1

1

I created a sample df that tried to look similar to yours (see below).

df

      Unnamed: 0  2010  2011  2012  2013  ...  2016  2017  2018  2019  TTM
0   gross profit    10    11    12    13  ...    16    17    18    19  300
1  total revenue     1     2     3     4  ...     7     8     9    10  400

The aim now would be to add a row between them ('gross'), with the values you have listed in the picture.

One way to add the row could be with numpy.insert, which returns an array back so you have to convert back to a pd.DataFrame:

# Store the columns of your df
cols = df.columns

# Add the row (the number indicates the index position for the row to be added,1 is the 2nd row as Python indexes start from 0)
new = pd.DataFrame(np.insert
                   (df.values, 1, values = ['gross',22, 45, 65,87,108,130,151,152,156,135,133], axis=0),
                   columns=cols)

Which gets back:

new

      Unnamed: 0 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019  TTM
0   gross profit   10   11   12   13   14   15   16   17   18   19  300
1          gross   22   45   65   87  108  130  151  152  156  135  133
2  total revenue    1    2    3    4    5    6    7    8    9   10  400

Hopefully this will work for you. Let me know for issues.

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you, I wrote it like that : income_statement=pd.DataFrame(np.insert(income_statement.values,1,values= gross,axis=0),columns=cols) in values i only put the array. and it removed the index column so instead of "Gross Profile", "Total Revenue" etc i got only the numbers. any idea how i can fix it?
It is because insert returns an array and you have to convert back to a dataframe. That is why I wrap the command with pd.DataFrame.... Did you also run the cols = df.columns? And is your dataframe also called df?
yes and my dataframe called income_statement. income_statement=pd.read_csv('company.csv',index_col=0,skipinitialspace=True) cols=income_statement.columns
Try changing cols=df.columns to cols=income_statement.columns and try it again?
wrote it like this: income_statement=pd.read_csv('company.csv',index_col=0,skipinitialspace=True) cols=income_statement.columns
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.