英文:
Append value to df row using lambda if else
问题
你好,专家们,我在尝试将值附加到DataFrame的新列时遇到了问题,需要通过比较其他列的值来实现。我搜索了相关的问题,但没有找到合适的答案。我是Python的新手。
我想通过比较DataFrame的其他列的最后4个值,使用df.apply
和lambda
来在每个元素的最后一行附加一个新的DataFrame列。在这方面的帮助将不胜感激。
我有以下DataFrame:
Symbol open close sig
0 APPL 153.60 152.90 0
1 APPL 152.90 153.55 1
2 APPL 153.55 152.00 0
3 APPL 152.00 153.50 1
4 APPL 153.50 154.10 1
5 TSLA 193.00 192.10 0
6 TSLA 192.10 191.50 0
7 TSLA 191.50 192.90 1
8 TSLA 192.90 192.45 0
9 TSLA 192.45 191.10 0
我想要比较df['sig']
列,除了第1行,以此类推,对于所有的股票,如果sig
列的最后4个值为APPL是1011,那么在第4行应该附加为1(即每支股票的最后一行)。如果TSLA的sig
列的最后4个值为0100,那么在第9行应该附加为0。
通过使用lambda函数或df.npwhere
等方法...
谢谢!
期望的结果如下:
Symbol open close sig signal
0 APPL 153.60 152.90 0 NaN
1 APPL 152.90 153.55 1 NaN
2 APPL 152.75 152.00 0 NaN
3 APPL 153.00 153.50 1 NaN
4 APPL 153.50 154.10 1 1
5 TSLA 193.00 192.10 0 NaN
6 TSLA 192.10 191.50 0 NaN
7 TSLA 191.50 192.90 1 NaN
8 TSLA 192.90 192.45 0 NaN
9 TSLA 192.45 191.10 0 0
英文:
hello experts i was struck in appending value to new column of df by comparing other column values. i searched related questions but not found suitable answer. I'm newbie to python
I want to append new column of df at last row of each element by comparing last 4 values of df other column using df.apply lambda help in this regard highly appreciated.
I have following df:
Symbol open close sig
0 APPL 153.60 152.90 0
1 APPL 152.90 153.55 1
2 APPL 153.55 152.00 0
3 APPL 152.00 153.50 1
4 APPL 153.50 154.10 1
5 TSLA 193.00 192.10 0
6 TSLA 192.10 191.50 0
7 TSLA 191.50 192.90 1
8 TSLA 192.90 192.45 0
9 TSLA 192.45 191.10 0
i want to compare df['sig'] column except 1 row and so on for all stocks, if sig column last 4 values APPL is 1011 then df ['signal'] at row 4 it should be appended as 1 (i.e. last row of each stock) if last 4 values df['sig'] of TSLA is 0100 then at 9 row df['signal'] to be appended as 0
by using lambda or df.npwhere etc...
Thanks!
expected this:
Symbol open close sig signal
0 APPL 153.60 152.90 0 NaN
1 APPL 152.90 153.55 1 NaN
2 APPL 152.75 152.00 0 NaN
3 APPL 153.00 153.50 1 NaN
4 APPL 153.50 154.10 1 1
5 TSLA 193.00 192.10 0 NaN
6 TSLA 192.10 191.50 0 NaN
7 TSLA 191.50 192.90 1 NaN
8 TSLA 192.90 192.45 0 NaN
9 TSLA 192.45 191.10 0 0
答案1
得分: 1
以下是您要翻译的内容:
You can use a groupby.rolling
:
df['signal'] = (df.groupby('Symbol')['sig'].rolling(4)
.apply(lambda x: x.eq([1, 0, 1, 1]).all()).droplevel(0)
)
Output:
Symbol open close sig signal
0 APPL 153.60 152.90 0 NaN
1 APPL 152.90 153.55 1 NaN
2 APPL 152.75 152.00 0 NaN
3 APPL 153.00 153.50 1 0.0
4 APPL 153.50 154.10 1 1.0
5 TSLA 193.00 192.10 0 NaN
6 TSLA 192.10 191.50 0 NaN
7 TSLA 191.50 192.90 1 NaN
8 TSLA 192.90 192.45 0 0.0
9 TSLA 192.45 191.10 0 0.0
mapping custom values for each group:
mapper = {'APPL': ([1, 0, 1, 1], 1), 'TSLA': ([0, 1, 0, 0], 0)}
df['signal'] = (
df.groupby('Symbol')['sig']
.apply(lambda g: g.rolling(4)
.apply(lambda x: mapper[g.name][1]
if x.tolist() == mapper[g.name][0]
else float('nan')
)
)
)
Output:
Symbol open close sig signal
0 APPL 153.60 152.90 0 NaN
1 APPL 152.90 153.55 1 NaN
2 APPL 153.55 152.00 0 NaN
3 APPL 152.00 153.50 1 NaN
4 APPL 153.50 154.10 1 1.0
5 TSLA 193.00 192.10 0 NaN
6 TSLA 192.10 191.50 0 NaN
7 TSLA 191.50 192.90 1 NaN
8 TSLA 192.90 192.45 0 NaN
9 TSLA 192.45 191.10 0 0.0
英文:
You can use a groupby.rolling
:
df['signal'] = (df.groupby('Symbol')['sig'].rolling(4)
.apply(lambda x: x.eq([1, 0, 1, 1]).all()).droplevel(0)
)
Output:
Symbol open close sig signal
0 APPL 153.60 152.90 0 NaN
1 APPL 152.90 153.55 1 NaN
2 APPL 152.75 152.00 0 NaN
3 APPL 153.00 153.50 1 0.0
4 APPL 153.50 154.10 1 1.0
5 TSLA 193.00 192.10 0 NaN
6 TSLA 192.10 191.50 0 NaN
7 TSLA 191.50 192.90 1 NaN
8 TSLA 192.90 192.45 0 0.0
9 TSLA 192.45 191.10 0 0.0
mapping custom values for each group:
mapper = {'APPL': ([1, 0, 1, 1], 1), 'TSLA': ([0, 1, 0, 0], 0)}
df['signal'] = (
df.groupby('Symbol')['sig']
.apply(lambda g: g.rolling(4)
.apply(lambda x: mapper[g.name][1]
if x.tolist() == mapper[g.name][0]
else float('nan')
)
)
)
Output:
Symbol open close sig signal
0 APPL 153.60 152.90 0 NaN
1 APPL 152.90 153.55 1 NaN
2 APPL 153.55 152.00 0 NaN
3 APPL 152.00 153.50 1 NaN
4 APPL 153.50 154.10 1 1.0
5 TSLA 193.00 192.10 0 NaN
6 TSLA 192.10 191.50 0 NaN
7 TSLA 191.50 192.90 1 NaN
8 TSLA 192.90 192.45 0 NaN
9 TSLA 192.45 191.10 0 0.0
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论