Append series to dict or list or dataframe from for loop?

我们两清 提交于 2019-12-12 06:49:37

问题


I have created a for loop which gives an out like this:

       SSTIME
SCODE        
0           0
1          57
3         202

I did reset_index on this series and i got this:

   SCODE  SSTIME
0      0       0
1      1      57
2      3     202

I want to append each result into a dataframe which will have column of scode and sstime.

P.S: SCODE can be of different length.

My objective is to create a Dataframe from that results.

Any help would be appreciated.


回答1:


I think you need not reset_index, but concat of list of Series:

list_ser = []
#sample loop for crate Series
for x in L:
    s = create_function()
    list_ser.append(s)

df = pd.concat(list_ser)



回答2:


You can convert each of these dataframes to a dictionary with pandas.DataFrame.to_dict()

And afterwards add to a list:

result = []
for x in range(1,13):
    result.append(x)


来源:https://stackoverflow.com/questions/45461346/append-series-to-dict-or-list-or-dataframe-from-for-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!