如何让 np.where 接受更多参数,以便它过滤 >、< 和 =;不仅仅是 > 和 <?
How do I get np.where to accept more arguments, so it will filter >, < , and =; not just > and <?
我正在使用 python 3.5 并导入了 numpy 和 pandas 库。我创建了一个名为 df 的 DataFrame,它有一个从零开始的索引和两列;变化百分比 (PofChg) 和上升、下降或持平 (U_D_F)。
对于 U_D_F 列,我想根据 PofChg 列用词 'Up'、'Down'、'Flat' 填充它。向上表示大于零,向下表示小于零,平坦表示等于零。
np.where 函数似乎运行良好,除了两点,
(1)为什么PofChg栏的数字是"Zero",而U_D_F栏显示"Down"
(2) 如何使 np.where 函数接受更多参数,即而不是说 - 如果 df.PofChg is > 0 ,如果为真显示 "Up" 或者如果为假显示 "Down" ,我想将其更改为 - 如果 df.PofChg > 0,如果为真显示 "Up" 或如果为假显示 "Down",但如果它等于零则显示 "Flat"
这是我打印df时的当前输出
PofChg U_D_F
0 -1 Down
1 0 Down
2 1 Up
3 -2 Down
4 0 Down
5 5 Up
6 3 Up
7 -6 Down
Press any key to continue . . .
这是我的代码
import pandas as pd
import numpy as np
df = pd.DataFrame({'PofChg':[-1,0,1,-2,0,5,3,-6]})
df['U_D_F'] = np.where(df.PofChg > 0 , 'Up','Down');df
print(df)
在这种情况下,我会结合使用 map() 和 np.sign()
:
In [133]: mp = {-1:'Down', 0:'Flat', 1:'Up'}
In [134]: df['U_D_F'] = np.sign(df.PofChg).map(mp)
In [135]: df
Out[135]:
PofChg U_D_F
0 -1 Down
1 0 Flat
2 1 Up
3 -2 Down
4 0 Flat
5 5 Up
6 3 Up
7 -6 Down
np.sign():
In [136]: np.sign(df.PofChg)
Out[136]:
0 -1
1 0
2 1
3 -1
4 0
5 1
6 1
7 -1
Name: PofChg, dtype: int64
np.sign(df.PofChg)
的类型:
In [9]: type(np.sign(df.PofChg))
Out[9]: pandas.core.series.Series
Why is it displaying "Down" in the U_D_F column when the number in the PofChg column is "Zero"
那是因为你np.where
的条件是>0,所以,如果是0,条件就失败了,它会选择备选方案。
I want to change it to - if df.PofChg is > 0, if true display "Up" or if false display "Down", but if it is equal to zero then Display "Flat"
np.where(df.PofChg > 0 , 'Up', np.where(df.PofChg == 0, 'Flat', 'Down'))
如果df.PofChg > 0
,则选择'Up'
;否则,如果 df.PofChg == 0
,它选择 'Flat'
,否则选择 'Down'
。
我正在使用 python 3.5 并导入了 numpy 和 pandas 库。我创建了一个名为 df 的 DataFrame,它有一个从零开始的索引和两列;变化百分比 (PofChg) 和上升、下降或持平 (U_D_F)。
对于 U_D_F 列,我想根据 PofChg 列用词 'Up'、'Down'、'Flat' 填充它。向上表示大于零,向下表示小于零,平坦表示等于零。
np.where 函数似乎运行良好,除了两点, (1)为什么PofChg栏的数字是"Zero",而U_D_F栏显示"Down" (2) 如何使 np.where 函数接受更多参数,即而不是说 - 如果 df.PofChg is > 0 ,如果为真显示 "Up" 或者如果为假显示 "Down" ,我想将其更改为 - 如果 df.PofChg > 0,如果为真显示 "Up" 或如果为假显示 "Down",但如果它等于零则显示 "Flat"
这是我打印df时的当前输出
PofChg U_D_F
0 -1 Down
1 0 Down
2 1 Up
3 -2 Down
4 0 Down
5 5 Up
6 3 Up
7 -6 Down
Press any key to continue . . .
这是我的代码
import pandas as pd
import numpy as np
df = pd.DataFrame({'PofChg':[-1,0,1,-2,0,5,3,-6]})
df['U_D_F'] = np.where(df.PofChg > 0 , 'Up','Down');df
print(df)
在这种情况下,我会结合使用 map() 和 np.sign()
:
In [133]: mp = {-1:'Down', 0:'Flat', 1:'Up'}
In [134]: df['U_D_F'] = np.sign(df.PofChg).map(mp)
In [135]: df
Out[135]:
PofChg U_D_F
0 -1 Down
1 0 Flat
2 1 Up
3 -2 Down
4 0 Flat
5 5 Up
6 3 Up
7 -6 Down
np.sign():
In [136]: np.sign(df.PofChg)
Out[136]:
0 -1
1 0
2 1
3 -1
4 0
5 1
6 1
7 -1
Name: PofChg, dtype: int64
np.sign(df.PofChg)
的类型:
In [9]: type(np.sign(df.PofChg))
Out[9]: pandas.core.series.Series
Why is it displaying "Down" in the U_D_F column when the number in the PofChg column is "Zero"
那是因为你np.where
的条件是>0,所以,如果是0,条件就失败了,它会选择备选方案。
I want to change it to - if df.PofChg is > 0, if true display "Up" or if false display "Down", but if it is equal to zero then Display "Flat"
np.where(df.PofChg > 0 , 'Up', np.where(df.PofChg == 0, 'Flat', 'Down'))
如果df.PofChg > 0
,则选择'Up'
;否则,如果 df.PofChg == 0
,它选择 'Flat'
,否则选择 'Down'
。