Yas*_*ade 4 python dataframe python-3.x pandas
如何打印/返回特定值的索引?
movies_per_year = pd.DataFrame(movies_per_year)
movies_per_year.columns = ["count"]
print(movies_per_year)
count
year
1891 1
1893 1
1894 2
1895 2
1896 2
1898 5
Run Code Online (Sandbox Code Playgroud)
在这里,我的索引是year。我想返回 count 为 1 的所有索引。此外,movies_per_year.max()返回5。所以,它应该返回1898。
np.where() - 返回满足给定条件的元素的位置索引:
In [31]: np.where(df['count'] == 1)[0]
Out[31]: array([0, 1], dtype=int64)
Run Code Online (Sandbox Code Playgroud)
或者
In [35]: np.nonzero(df['count'] == 1)
Out[35]: (array([0, 1], dtype=int64),)
Run Code Online (Sandbox Code Playgroud)
如果您需要真正的索引值(标签)而不是它们的位置:
In [40]: df.index[df['count'] == 1]
Out[40]: Int64Index([1891, 1893], dtype='int64', name='year')
Run Code Online (Sandbox Code Playgroud)
查找最大元素的索引:
In [32]: df['count'].idxmax()
Out[32]: 1898
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6727 次 |
| 最近记录: |