gpt4 book ai didi

python - 从特定列向数据帧添加行

转载 作者:行者123 更新时间:2023-12-01 08:38:45 25 4
gpt4 key购买 nike

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

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 从 column=2018-11-23 插入到 df1 中我使用了 concat 、 insert 和append 但结果总是 false

最佳答案

问题在于对齐,两个 DataFrames 中都需要 DatetimeIndex

<小时/>

首先建议转置为T索引中 DatetimeIndexDataFrame:

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 DataFrame:

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

上次使用concataxis=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

关于python - 从特定列向数据帧添加行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53582814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com