我必须将dataFrame插入另一个 这是我的第一个数据帧:
kable
我要在其中插入此dataFrame
x=donne['Time Series (Daily)']
df1 = pd.DataFrame(x)
df1 = df1.rename(index={'2. high':'Hight','3. low':'Low'})
df1.loc['Hight']=df1.loc['Hight'].astype(float)
df1.loc['Low']=df1.loc['Low'].astype(float)
df1.loc['H+L']=(df1.loc['Hight'] + df1.loc['Low'])/2
df1.loc['sma']=0
df1
我想将concat从column = 2018-11-23插入df1 我使用了concat,插入和追加,但结果始终为假
答案 0 :(得分:1)
问题是对齐,两个DatetimeIndex
中都需要DataFrames
。
首先建议对索引中的DataFrame
用T
DatetimeIndex
换位:
x=donne['Time Series (Daily)']
#transpose
df1 = pd.DataFrame(x).T
#rename columns
df1 = df1.rename(columns={'2. high':'Hight','3. low':'Low'})
#remove loc because working with columns
df1['Hight']=df1['Hight'].astype(float)
df1['Low']=df1['Low'].astype(float)
df1['H+L']=(df1['Hight'] + df1['Low'])/2
df1['sma']=0
然后通过转置和sma
更改DatetimeIndex
DataFrame:
sma = sma.T.set_index(0)[1].rename('sma').astype(float)
sma.index = pd.to_datetime(sma.index)
最后concat
与axis=1
一起使用,因为新列:
df = pd.concat([df1, sma], axis=1)
或分配:
df1['sma'] = sma
示例:
idx = pd.date_range('2001-01-01', periods=3)
df1 = pd.DataFrame({'2. high':[2,3,4],
'3. low':[1,2,3]}, index=idx)
print (df1)
2. high 3. low
2001-01-01 2 1
2001-01-02 3 2
2001-01-03 4 3
df1 = df1.rename(columns={'2. high':'Hight','3. low':'Low'})
#remove loc because working with columns
df1['Hight']=df1['Hight'].astype(float)
df1['Low']=df1['Low'].astype(float)
df1['H+L']=(df1['Hight'] + df1['Low'])/2
df1['sma']=0
print (df1)
Hight Low H+L sma
2001-01-01 2.0 1.0 1.5 0
2001-01-02 3.0 2.0 2.5 0
2001-01-03 4.0 3.0 3.5 0
sma = pd.DataFrame([['2001-01-01','2001-01-02','2001-01-03'],
[12,34,56]])
print (sma)
0 1 2
0 2001-01-01 2001-01-02 2001-01-03
1 12 34 56
sma = sma.T.set_index(0)[1].rename('sma').astype(float)
sma.index = pd.to_datetime(sma.index)
print (sma)
2001-01-01 12
2001-01-02 34
2001-01-03 56
Name: sma, dtype: object
df1['sma'] = sma
print (df1)
Hight Low H+L sma
2001-01-01 2.0 1.0 1.5 12
2001-01-02 3.0 2.0 2.5 34
2001-01-03 4.0 3.0 3.5 56
如果在列中确实需要DatetimeIndex
:
idx = pd.date_range('2001-01-01', periods=3)
df1 = pd.DataFrame({'2. high':[2,3,4],
'3. low':[1,2,3]}, index=idx).T
print (df1)
2001-01-01 2001-01-02 2001-01-03
2. high 2 3 4
3. low 1 2 3
df1 = df1.rename(index={'2. high':'Hight','3. low':'Low'})
df1.loc['Hight']=df1.loc['Hight'].astype(float)
df1.loc['Low']=df1.loc['Low'].astype(float)
df1.loc['H+L']=(df1.loc['Hight'] + df1.loc['Low'])/2
print (df1)
2001-01-01 2001-01-02 2001-01-03
Hight 2.0 3.0 4.0
Low 1.0 2.0 3.0
H+L 1.5 2.5 3.5
sma = pd.DataFrame([['2001-01-01','2001-01-02','2001-01-03'],
[12,34,56]])
print (sma)
0 1 2
0 2001-01-01 2001-01-02 2001-01-03
1 12 34 56
sma = sma.T.set_index(0)[[1]].T.rename({1:'sma'})
sma.columns = pd.to_datetime(sma.columns)
print (sma)
0 2001-01-01 2001-01-02 2001-01-03
sma 12 34 56
df = pd.concat([df1, sma], axis=0)
print (df)
0 2001-01-01 2001-01-02 2001-01-03
Hight 2 3 4
Low 1 2 3
H+L 1.5 2.5 3.5
sma 12 34 56