我認爲你需要boolean indexing
與isin
與np.arange
或range
:
print (np.arange(5,51))
[ 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50]
print (dfa[dfa.a.isin(np.arange(5,51))])
或者:
print (dfa[dfa.a.isin(range(5,51))])
解決方案與between
:
print (dfa[dfa['a'].between(5, 50)])
樣品(一個值更改爲8
):
dfa = pd.DataFrame({'a':[1,2,3,8],'b':[4,5,7,6]})
print (dfa)
a b
0 1 4
1 2 5
2 3 7
3 8 6
print (dfa[dfa.a.isin(np.arange(5,51))])
a b
3 8 6
print (dfa[dfa.a.isin(range(5,51))])
a b
3 8 6
print (dfa[dfa['a'].between(5, 50)])
a b
3 8 6
我不知道,如果知道 - 就是我的回答是否正確? – jezrael