如何使用(1,20)DataFrames作为行正确创建数组?

时间:2015-12-04 13:32:56

标签: python numpy pandas dataframe

flights = []
for index, row in flights_arrivals.iterrows():
    result = find(row,flights_departures)
    if len(result) > 0:
        flights.append(result)

result是一个DataFrame(result.shape(1,20)),flights是一个列表,其中每一行都是一个DataFrame。换句话说,flights[0]会返回一个DataFrame。

我希望flights成为一个数组,以便我可以使用索引检索数据,例如flights[0][3]flights[1][5]。 如果我追加result,如何将flights转换为包含列的行?

result在函数find中以这种方式创建:

mergedrow = pd.DataFrame()
featuresA = [str(col) + '_x' for col in group.index]
featuresB = [str(col) + '_y' for col in match.iloc[[idx]].columns]
mergedrow[featuresA] = pd.DataFrame([group.values], columns = featuresA)
mergedrow[featuresB] = pd.DataFrame([match.iloc[idx].values], columns = featuresB)
return mergedrow

1 个答案:

答案 0 :(得分:0)

如果您有一个包含5个数据框的列表:

a=[pd.DataFrame(np.random.random((1,20))) for x in range(5)]

您可以使用vstack将它们整理成一个数组:

b=np.vstack(a)

array([[ 0.23681401,  0.08313223,  0.84855748,  0.34495448,  0.53298811,
         0.57441147,  0.64085605,  0.90340984,  0.58100924,  0.85655169,
         0.62375155,  0.32812196,  0.15694653,  0.48810121,  0.95320749,
         0.14490556,  0.92484754,  0.72503867,  0.19851095,  0.50123389],
       [ 0.88966717,  0.26479057,  0.47082078,  0.01441357,  0.82842833,
         0.37878295,  0.83353233,  0.1568765 ,  0.18378868,  0.66754036,
         0.96575879,  0.06526984,  0.70848796,  0.99404839,  0.49494481,
         0.10885664,  0.113089  ,  0.67820823,  0.31003583,  0.91319635],
       [ 0.24681306,  0.03665718,  0.10614816,  0.3846326 ,  0.68164136,
         0.49851296,  0.14622779,  0.42610646,  0.41415886,  0.95512411,
         0.9355059 ,  0.73846798,  0.14842677,  0.27546837,  0.07586456,
         0.76220594,  0.88244072,  0.14982746,  0.61480415,  0.5114436 ],
       [ 0.47833991,  0.46069876,  0.3411488 ,  0.97806519,  0.10420578,
         0.27032901,  0.15227294,  0.16389061,  0.39181361,  0.40418859,
         0.96420969,  0.48299452,  0.40344904,  0.01519821,  0.93852375,
         0.45037877,  0.3175535 ,  0.61617126,  0.23889963,  0.55793313],
       [ 0.22325491,  0.23077949,  0.96176709,  0.9086851 ,  0.20877316,
         0.2178402 ,  0.4446958 ,  0.53089031,  0.36595776,  0.57019218,
         0.02362881,  0.6274627 ,  0.77485623,  0.35006059,  0.08074195,
         0.39227807,  0.6605864 ,  0.48477404,  0.49180667,  0.12289587]])