如何用另一个数据框的值替换PANDAS DataFrame?
我有2个dataframes(signal_df
和price_df
),可以使用以下代码生成。
import pandas as pd
import numpy as np
signal_df = pd.DataFrame({
'long':[
True ,True, np.nan, True, np.nan
],
'short':[
np.nan, np.nan, True, np.nan, True
],
'date':[
'2020-01-01 19:15', '2020-01-01 20:00', '2020-01-01 22:15', '2020-01-01 22:45', '2020-01-02 00:30',
],
})
# convert the column (it's a string) to datetime type
datetime_series = pd.to_datetime(signal_df['date'])
# create datetime index passing the datetime series
datetime_index = pd.DatetimeIndex(datetime_series.values)
signal_df = signal_df.set_index(datetime_index)
signal_df.drop('date',axis=1,inplace=True)
print(signal_df)
price_df = pd.DataFrame({
'close_price':[
30, 2, 3, 29,
15, 6, 19, 56,
9 , 38, 41, 12,
23, 14, 15, 16,
38, 18, 19, 20,
21, 22, 23, 33,
25, 26, 10, 28
],
'date':[
'2020-01-01 19:00', '2020-01-01 19:15', '2020-01-01 19:30', '2020-01-01 19:45',
'2020-01-01 20:00', '2020-01-01 20:15', '2020-01-01 20:30', '2020-01-01 20:45',
'2020-01-01 21:00', '2020-01-01 21:15', '2020-01-01 21:30', '2020-01-01 21:45',
'2020-01-01 22:00', '2020-01-01 22:15', '2020-01-01 22:30', '2020-01-01 22:45',
'2020-01-01 23:00', '2020-01-01 23:15', '2020-01-01 23:30', '2020-01-01 23:45',
'2020-01-02 00:00', '2020-01-02 00:15', '2020-01-02 00:30', '2020-01-02 00:45',
'2020-01-02 01:00', '2020-01-02 01:15', '2020-01-02 01:30', '2020-01-02 01:45',
]
})
# convert the column (it's a string) to datetime type
datetime_series = pd.to_datetime(price_df['date'])
# create datetime index passing the datetime series
datetime_index = pd.DatetimeIndex(datetime_series.values)
price_df = price_df.set_index(datetime_index)
price_df.drop('date',axis=1,inplace=True)
print(price_df)
如何创建看起来如下的第三个DataFrame(new_df
)?
new_df
long
2020-01-01 19:15:00 2
2020-01-01 20:00:00 15
2020-01-01 22:15:00 NaN
2020-01-01 22:45:00 16
2020-01-02 00:30:00 NaN
new_df
是通过首先查找true
值创建的Price_df
中的相应值。
I have 2 DataFrames (signal_df
, and price_df
) that can be generated using the following code.
import pandas as pd
import numpy as np
signal_df = pd.DataFrame({
'long':[
True ,True, np.nan, True, np.nan
],
'short':[
np.nan, np.nan, True, np.nan, True
],
'date':[
'2020-01-01 19:15', '2020-01-01 20:00', '2020-01-01 22:15', '2020-01-01 22:45', '2020-01-02 00:30',
],
})
# convert the column (it's a string) to datetime type
datetime_series = pd.to_datetime(signal_df['date'])
# create datetime index passing the datetime series
datetime_index = pd.DatetimeIndex(datetime_series.values)
signal_df = signal_df.set_index(datetime_index)
signal_df.drop('date',axis=1,inplace=True)
print(signal_df)
price_df = pd.DataFrame({
'close_price':[
30, 2, 3, 29,
15, 6, 19, 56,
9 , 38, 41, 12,
23, 14, 15, 16,
38, 18, 19, 20,
21, 22, 23, 33,
25, 26, 10, 28
],
'date':[
'2020-01-01 19:00', '2020-01-01 19:15', '2020-01-01 19:30', '2020-01-01 19:45',
'2020-01-01 20:00', '2020-01-01 20:15', '2020-01-01 20:30', '2020-01-01 20:45',
'2020-01-01 21:00', '2020-01-01 21:15', '2020-01-01 21:30', '2020-01-01 21:45',
'2020-01-01 22:00', '2020-01-01 22:15', '2020-01-01 22:30', '2020-01-01 22:45',
'2020-01-01 23:00', '2020-01-01 23:15', '2020-01-01 23:30', '2020-01-01 23:45',
'2020-01-02 00:00', '2020-01-02 00:15', '2020-01-02 00:30', '2020-01-02 00:45',
'2020-01-02 01:00', '2020-01-02 01:15', '2020-01-02 01:30', '2020-01-02 01:45',
]
})
# convert the column (it's a string) to datetime type
datetime_series = pd.to_datetime(price_df['date'])
# create datetime index passing the datetime series
datetime_index = pd.DatetimeIndex(datetime_series.values)
price_df = price_df.set_index(datetime_index)
price_df.drop('date',axis=1,inplace=True)
print(price_df)
How can I create a third DataFrame (new_df
) that looks like the following?
new_df
long
2020-01-01 19:15:00 2
2020-01-01 20:00:00 15
2020-01-01 22:15:00 NaN
2020-01-01 22:45:00 16
2020-01-02 00:30:00 NaN
new_df
is created by first looking for the True
values in signal_df
's long
column, then replacing them with the corresponding values in price_df
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以做:
输出:
You can do:
output:
您可以尝试
bask
long
signal> signal_df
列中的真实值You can try
mask
the True value inlong
column ofsignal_df