0

I'm new to python so this might seem a bit easy problem for you expertise.

I've 6 categories(0 to 5),each of which has 4 sub-categories namely: '3','4','5','6'.

for this,I've created a dataframe using:

df=pd.DataFrame(index=list(range(5)),columns=list([3,4,5,6])

Now,I'm getting some calculated values from my loop:

for i in range(5):
    for j in list([3,4,5,6]):
       somecalculation=a

Now,I'm trying to replace the values of df with these calculations like for second iteration (i.e. for i=0,j=4), I got somecalculation=b, for third somecalculation=c and further d. When loop again iterates over i=2,I get calculations as e,f,g,h and so on for further iterations. I'm trying to append these values to df as soon as I obtain them but I'm not getting the desired output as

  3 4 5 6 
0 a b c d
1 e f g h
2 i j k l
.........
.........
.........

because ultimately,I want to take average of the column values using their indices, but replacing values of dataframe is becoming troublesome.

2 Answers 2

1

your suggestion of appending to DataFrame rows iteratively is not optimal. it will slowdown the code. instead you can append the output to a list and than reshape the list as you like and ultimately convert it to pd.DataFrame. that will be way faster than what you propose. for example:

import pandas as pd, numpy as np
list1=[] #initialize the list
list1.extend([i]) #where i is the output from your loop
df = pd.DataFrame(np.reshape(list1,(5,4)),columns=['a','b','c','d']) # finally you can reshape the list as desired (eg: 5 row X 4 cols) and write it to dataframe in a single step

if this helps please do not forget to accept it:)

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

Comments

0

Not sure if I understand your question correctly, but there are multiple ways to insert a value to a desired dataframe cell.

For example line,

df.xs(1)['e'] = x

inserts value x to a cell that has an index value of 1 and is in column 'e'.

If you wan't to calculate the mean of the certain column and insert that to spesific cell, you could do:

import numpy as np

df.xs(desired_index_value)['desired_column_name'] = np.mean(df.xs('col_x',axis=1))

1 Comment

I don't want to mention every time that I need to replace this or that value of the dataframe.I want my calculations to automatically replace the values of the df,once they are calculated in one iteration

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.