考虑pandas中的以下数据框
date action price
20150101 buy 10
20150102 buy 9
20150103 sell 11
20150104 sell 10
20150105 buy 8
20150106 sell 9
如果我想在每次出售'时添加行转入“购买”,插入行只是上一行的副本,除了更改' sell'进入'购买'如下:
date action price
20150101 buy 10
20150102 buy 9
20150103 sell 11
20150104 sell 10
**20150104 buy 10**
20150105 buy 8
20150106 sell 9
**20150106 buy 9 **
感谢您的帮助。
答案 0 :(得分:3)
您可以使用
标识转换行mask = (df['action'] == 'sell') & (df['action'].shift(-1) != 'sell')
# In [229]: mask
# Out[229]:
# 0 False
# 1 False
# 2 False
# 3 True
# 4 False
# 5 True
# Name: action, dtype: bool
然后你可以创建一个新的DataFrame,其中包含mask
为True的行:
new = df.loc[mask].copy()
将操作设置为'buy'
:
new['action'] = 'buy'
# date action price
# 3 20150104 buy 10
# 5 20150106 buy 9
构建一个新的DataFrame,用于连接df
和new
:
df = pd.concat([df, new])
并按date
排序:
df = df.sort(['date'])
例如,
import pandas as pd
df = pd.read_table('data', sep='\s+')
mask = (df['action'] == 'sell') & (df['action'].shift(-1) != 'sell')
new = df.loc[mask].copy()
new['action'] = 'buy'
df = pd.concat([df, new])
df = df.sort(['date'])
df = df.reset_index(drop=True)
print(df)
产量
date action price
0 20150101 buy 10
1 20150102 buy 9
2 20150103 sell 11
3 20150104 sell 10
4 20150104 buy 10
5 20150105 buy 8
6 20150106 sell 9
7 20150106 buy 9