Issue
I have a Data Frame
called Data
. I wrote this filter for filter the rows:
data[data["Grow"] >= 1.5]
It returned some rows like these:
PriceYesterday Open High Low
------------------------------------------------------------------
7 6888.0 6881.66 7232.0 6882.0
53 7505.0 7555.72 7735.0 7452.0
55 7932.0 8093.08 8120.0 7974.0
64 7794.0 7787.29 8001.0 7719.0
...
As you see there are some rows in the indexes 7, 53, 55 ,...
. Now I want to get rows in indexes 8, 54, 56, ...
too. Is there any straight forward way to do this? Thanks
Solution
You can use Index.intersection
for avoid error if matching last row and want select not exist index values:
data = pd.DataFrame({
'A':list('abcdef'),
'B':[4,5,4,5,5,4],
'Grow':[0,8,2,0.4,2,3.3],
})
df1 = data[data["Grow"] >= 1.5]
print (df1)
A B Grow
1 b 5 8.0
2 c 4 2.0
4 e 5 2.0
5 f 4 3.3
df2 = data.loc[data.index.intersection(df1.index + 1)]
print (df2)
A B Grow
2 c 4 2.0
3 d 5 0.4
5 f 4 3.3
Another idea is select by shifted values by Series.shift
df1 = data[data["Grow"] >= 1.5]
df2 = data[data["Grow"].shift() >= 1.5]
print (df2)
A B Grow
2 c 4 2.0
3 d 5 0.4
5 f 4 3.3
df1 = data[data["Grow"] >= 1.5]
df2 = data.loc[df1.index + 1]
print (df2)
KeyError: "Passing list-likes to .loc or [] with any missing labels is no longer supported. The following labels were missing: Int64Index([6], dtype='int64'). See https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike"
Answered By - jezrael
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.