我有一个数据框
menu.seePrice(item)
是否有某种自定义过滤方法,让Python知道B> M> K +
说我想过滤, 0 1 2 3 Marketcap
0 1.707280 0.666952 0.638515 -0.061126 2.291747 1.71B
1 -1.017134 1.353627 0.618433 0.008279 0.148128 1.82B
2 -0.774057 -0.165566 -0.083345 0.741598 -0.139851 1.1M
3 -0.630724 0.250737 1.308556 -1.040799 1.064456 30.92M
4 2.029370 0.899612 0.261146 1.474148 -1.663970 476.74k
5 2.029370 0.899612 0.261146 1.474148 -1.663970 -1
,是否有一种聪明或干净的方式来做到这一点?
拥有M或B使得该值非常易读且易于区分。
谢谢。
编辑:重新打开线程作为Max U的答案,而优秀似乎产生了一个熊猫bug,我们在Github上打开了一个问题。答案 0 :(得分:3)
这不是超级干净,但它可以解决问题并且不会使用任何python迭代:
<强>代码:强>
# Create a separate column (which you can omit later) that converts 'Marketcap' strings to numbers
df['cap'] = df.loc[df['Marketcap'].str.contains('B'), 'Marketcap'].str.replace('B','').astype(float) * 1000
df['cap'].fillna(df.loc[df['Marketcap'].str.contains('M'), 'Marketcap'].str.replace('M',''), inplace = True)
# For pandas pre-0.20.0 (<May 2017)
print df.ix[df['cap'].astype(float) > 35, :-1]
# For pandas 0.20.0+ (.ix[] deprecated)
print df.iloc[df[df['cap'].astype(float) > 35].index, :-1]
# Or, alternate pandas 0.20.0+ option (thanks @Psidom)
print df[df['cap'].astype(float) > 35].iloc[:,:-1]
<强>输出:强>
0 1 2 3 4 Marketcap
0 1.707280 0.666952 0.638515 -0.061126 2.291747 1.71B
1 -1.017134 1.353627 0.618433 0.008279 0.148128 1.82B
4 2.029370 0.899612 0.261146 1.474148 -1.663970 100.9M
答案 1 :(得分:2)
<强>更新强>
In [44]: df
Out[44]:
0 1 2 3 4 Marketcap
0 1.707280 0.666952 0.638515 -0.061126 2.291747 1.71B
1 -1.017134 1.353627 0.618433 0.008279 0.148128 1.82B
2 -0.774057 -0.165566 -0.083345 0.741598 -0.139851 1.1M
3 -0.630724 0.250737 1.308556 -1.040799 1.064456 30.92M
4 2.029370 0.899612 0.261146 1.474148 -1.663970 476.74k
5 2.029370 0.899612 0.261146 1.474148 -1.663970 -1
In [45]: df[pd.eval(df.Marketcap.replace(['[Kk]','[Mm]','[Bb]'],
['*10**3','*10**6','*10**9'], regex=True) \
.add(' < 35*10**6'))]
Out[45]:
0 1 2 3 4 Marketcap
2 -0.774057 -0.165566 -0.083345 0.741598 -0.139851 1.1M
3 -0.630724 0.250737 1.308556 -1.040799 1.064456 30.92M
4 2.029370 0.899612 0.261146 1.474148 -1.663970 476.74k
5 2.029370 0.899612 0.261146 1.474148 -1.663970 -1
我这样做:
In [13]: df[pd.eval(df.Marketcap.replace(['M','B'],['','*1000'], regex=True).add(' > 35'))]
Out[13]:
0 1 2 3 4 Marketcap
0 1.707280 0.666952 0.638515 -0.061126 2.291747 1.71B
1 -1.017134 1.353627 0.618433 0.008279 0.148128 1.82B
4 2.029370 0.899612 0.261146 1.474148 -1.663970 100.9M
说明:
In [14]: df.Marketcap.replace(['M','B'],['','*1000'], regex=True)
Out[14]:
0 1.71*1000
1 1.82*1000
2 1.1
3 30.92
4 100.9
Name: Marketcap, dtype: object
In [15]: df.Marketcap.replace(['M','B'],['','*1000'], regex=True).add(' > 35')
Out[15]:
0 1.71*1000 > 35
1 1.82*1000 > 35
2 1.1 > 35
3 30.92 > 35
4 100.9 > 35
Name: Marketcap, dtype: object
In [16]: pd.eval(df.Marketcap.replace(['M','B'],['','*1000'], regex=True).add(' > 35'))
Out[16]: array([True, True, False, False, True], dtype=object)
答案 2 :(得分:2)
来源DF:
In [176]: df
Out[176]:
0 1 2 3 Market Cap
0 1.707280 0.666952 0.638515 -0.061126 2.291747 1.71B
1 -1.017134 1.353627 0.618433 0.008279 0.148128 1.82B
2 -0.774057 -0.165566 -0.083345 0.741598 -0.139851 1.1M
3 -0.630724 0.250737 1.308556 -1.040799 1.064456 30.92M
4 2.029370 0.899612 0.261146 1.474148 -1.663970 476.74k
5 2.029370 0.899612 0.261146 1.474148 -1.663970 -1
<强>解决方案:强>
to_replace = ['\d+\s*[Kk]','\d+\s*[Mm]','\d+\s*[Bb]', '-1', 'N/A']
value = [1000,1000000,1000000000, 1, 1]
mask = df.assign(
f=df['Market Cap'].replace(to_replace, value, regex=True),
Marketcap=pd.to_numeric(df['Market Cap'].str.replace(r'[^\d\.]', ''), errors='coerce')
).eval("Marketcap * f < 35000000")
df[mask]
<强>结果:强>
In [178]: df[mask]
Out[178]:
0 1 2 3 Market Cap
2 -0.774057 -0.165566 -0.083345 0.741598 -0.139851 1.1M
3 -0.630724 0.250737 1.308556 -1.040799 1.064456 30.92M
4 2.029370 0.899612 0.261146 1.474148 -1.663970 476.74k
5 2.029370 0.899612 0.261146 1.474148 -1.663970 -1
PS如果要在结果数据集更改中保留非数字值(如N/A
):
pd.to_numeric(df['Market Cap'].str.replace(r'[^\d\.]', ''), errors='coerce')
到
pd.to_numeric(df['Market Cap'].str.replace(r'[^\d\.]', ''), errors='coerce').fillna('0')