python - Get row index from DataFrame row -
is possible row number (i.e. "the ordinal position of index value") of dataframe row without adding row contains row number (the index can arbitrary, i.e. multiindex)?
>>> import pandas pd >>> df = pd.dataframe({'a': [2, 3, 4, 2, 4, 6]}) >>> result = df[df.a > 3] >>> result.iloc[0] 4 name: 2, dtype: int64 # how can original row index of iloc[0] in df? i have done df['row_index'] = range(len(df)) maintain original row number, wondering if pandas has built-in way of doing this.
access .name attribute , use get_loc:
in [10]: df.index.get_loc(result.iloc[0].name) out[10]: 2
Comments
Post a Comment