Python 如何在Dataframe中增量添加行?

Python 如何在Dataframe中增量添加行?,python,pandas,dataframe,append,Python,Pandas,Dataframe,Append,我正在计算9:15到15:30之间每15分钟的数据开放-高-低-关闭(OHLC),并希望将OHLC值存储在每一新行的数据帧中 ohlc = pd.DataFrame(columns=('Open','High','Low','Close')) for row in ohlc: ohlc.loc[10] = pd.DataFrame([[candle_open_price,candle_high_price,candle_low_price,candle_close_price]]) 但

我正在计算9:15到15:30之间每15分钟的数据开放-高-低-关闭(OHLC),并希望将OHLC值存储在每一新行的数据帧中

ohlc = pd.DataFrame(columns=('Open','High','Low','Close'))
for row in ohlc:
    ohlc.loc[10] = pd.DataFrame([[candle_open_price,candle_high_price,candle_low_price,candle_close_price]])
但我不能说得到以下错误:

ValueError: cannot set a row with mismatched columns
我只想增量存储我计算的每15分钟的OHLC数据,并将其放入新的OHLC数据帧的行中


编辑


IIUC您可以将每行的数据帧附加到数据帧列表
dfs
,然后将它们添加到
df1

ohlc = pd.DataFrame(columns=('Open','High','Low','Close'))

dfs = []
for row in ohlc.iterrows():
    df = pd.DataFrame([candle_open_price,candle_high_price,
                        candle_low_price,candle_close_price]).T
    dfs.append(df)

df1 = pd.concat(dfs, ignore_index=True)
print (df1)
然后
concat
到原始
DataFrame
ohlc

df2 = pd.concat([ohlc,df1])
print (df2)

样本(用于循环每次迭代中的测试,添加相同的数据):


df=pd.DataFrame([[candle\u open\u price,candle\u high\u price,candle\u low\u price,candle\u close\u price]]的输出是什么?
打印(df)
<代码>数据帧有一行吗?什么是
df.columns
?请注意,像这样将行添加到数据框中效率很低,因为会为每个新大小创建一个全新的数据框。您可以添加一些示例数据吗?5-6行。我也尝试过使用concat,但由于
无法连接非NDFrame对象而出现错误
我也添加了示例数据。对不起,我不理解
,而(first\u trd\u time
-如果,可能有
?我正在从第一个交易时间到最后一个交易时间浏览该列。
ohlc = pd.DataFrame(columns=('Open','High','Low','Close'))

dfs = []
for row in ohlc.iterrows():
    df = pd.DataFrame([candle_open_price,candle_high_price,
                        candle_low_price,candle_close_price]).T
    dfs.append(df)

df1 = pd.concat(dfs, ignore_index=True)
print (df1)
df2 = pd.concat([ohlc,df1])
print (df2)
#sample data
candle_open_price = pd.Series([1.5,10], 
                              name='Open', 
                              index=pd.DatetimeIndex(['2016-01-02','2016-01-03']) )
candle_high_price =  pd.Series([8,9], 
                               name='High', 
                               index=pd.DatetimeIndex(['2016-01-02','2016-01-03']))
candle_low_price =  pd.Series([0,12], 
                              name='Low', 
                              index=pd.DatetimeIndex(['2016-01-02','2016-01-03']))
candle_close_price =  pd.Series([4,5], 
                                name='Close', 
                                index=pd.DatetimeIndex(['2016-01-02','2016-01-03']))

data = np.array([[1,2,3,5],[7,7,8,9],[10,8,9,3]])
idx = pd.DatetimeIndex(['2016-01-08','2016-01-09','2016-01-10'])
ohlc = pd.DataFrame(data=data, 
                    columns=('Open','High','Low','Close'),
                    index=idx)
print (ohlc)
            Open  High  Low  Close
2016-01-08     1     2    3      5
2016-01-09     7     7    8      9
2016-01-10    10     8    9      3
dfs = []
for row in ohlc.iterrows():
    df = pd.DataFrame([candle_open_price,candle_high_price,
                       candle_low_price,candle_close_price]).T
    #print (df)
    dfs.append(df)

df1 = pd.concat(dfs)
print (df1)
            Open  High   Low  Close
2016-01-02   1.5   8.0   0.0    4.0
2016-01-03  10.0   9.0  12.0    5.0
2016-01-02   1.5   8.0   0.0    4.0
2016-01-03  10.0   9.0  12.0    5.0
2016-01-02   1.5   8.0   0.0    4.0
2016-01-03  10.0   9.0  12.0    5.0

df2 = pd.concat([ohlc,df1])
print (df2)
            Open  High   Low  Close
2016-01-08   1.0   2.0   3.0    5.0
2016-01-09   7.0   7.0   8.0    9.0
2016-01-10  10.0   8.0   9.0    3.0
2016-01-02   1.5   8.0   0.0    4.0
2016-01-03  10.0   9.0  12.0    5.0
2016-01-02   1.5   8.0   0.0    4.0
2016-01-03  10.0   9.0  12.0    5.0
2016-01-02   1.5   8.0   0.0    4.0
2016-01-03  10.0   9.0  12.0    5.0