loops - overwriting values in dataframe in python -
i'm new python might seem bit easy problem expertise.
i've 6 categories(0 5),each of has 4 sub-categories namely: '3','4','5','6'.
for this,i've created dataframe using:
df=pd.dataframe(index=list(range(5)),columns=list([3,4,5,6])
now,i'm getting calculated values loop:
for in range(5): j in list([3,4,5,6]): somecalculation=a
now,i'm trying replace values of df these calculations second iteration (i.e. for i=0,j=4
), got somecalculation=b
, third somecalculation=c
, further d. when loop again iterates on i=2
,i calculations e,f,g,h , on further iterations. i'm trying append these values df obtain them i'm not getting desired output
3 4 5 6 0 b c d 1 e f g h 2 j k l ......... ......... .........
because ultimately,i want take average of column values using indices, replacing values of dataframe becoming troublesome.
your suggestion of appending dataframe rows iteratively not optimal. slowdown code. instead can append output list , reshape list , convert pd.dataframe. way faster propose. example:
import pandas pd, numpy np list1=[] #initialize list list1.extend([i]) #where output loop df = pd.dataframe(np.reshape(list1,(5,4)),columns=['a','b','c','d']) # can reshape list desired (eg: 5 row x 4 cols) , write dataframe in single step
if helps please not forget accept it:)
Comments
Post a Comment