从特定列向数据帧添加行

2024-04-23 15:24:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我必须把一个数据帧插入另一个数据帧 这是我的第一个数据帧:

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

这就是结果 enter image description here

我想把这个数据框插进去

`frame=[date,sma]`
concat=pd.concat(frame,axis=0,ignore_index=True)
concat

enter image description here

我想将concat从列=2018-11-23插入df1 我使用concat、insert和append,但结果总是错误的


Tags: 数据indextimefloatframeloclowseries
1条回答
网友
1楼 · 发布于 2024-04-23 15:24:44

问题是对齐,两者都需要DatetimeIndex


首先建议通过^{}DataFrame转置索引中的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

然后用转置和DatetimeIndex更改sma数据帧:

sma = sma.T.set_index(0)[1].rename('sma').astype(float)
sma.index = pd.to_datetime(sma.index)

上次将^{}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

相关问题 更多 >