[英]Python pandas slice dataframe by multiple index ranges
按更多索引范圍(例如
10:12<\/code>和
25:28<\/code> )對數據幀進行切片的 pythonic 方法是什么?
我想要一種更優雅的方式:
df = pd.DataFrame({'a':range(10,100)})
df.iloc[[i for i in range(10,12)] + [i for i in range(25,28)]]
您可以使用 numpy 的r_
“切片技巧”:
df = pd.DataFrame({'a':range(10,100)})
df.iloc[pd.np.r_[10:12, 25:28]]
注意:現在發出警告The pandas.np module is deprecated and will be removed from pandas in a future version. Import numpy directly instead
The pandas.np module is deprecated and will be removed from pandas in a future version. Import numpy directly instead
。 為此,您可以import numpy as np
,然后按以下方式切片:
df.iloc[np.r_[10:12, 25:28]]
這給出:
a
10 20
11 21
25 35
26 36
27 37
您可以利用熊貓的 isin 功能。
df = pd.DataFrame({'a':range(10,100)})
ls = [i for i in range(10,12)] + [i for i in range(25,28)]
df[df.index.isin(ls)]
a
10 20
11 21
25 35
26 36
27 37
在@KevinOelen 使用 Panda 的 isin 函數的基礎上,這里有一種 Python 方式(Python 3.8)來查看 Pandas DataFrame 或 GeoPandas GeoDataFrame,只顯示幾行頭部和尾部。 此方法不需要導入 numpy。
要使用只需調用glance(your_df)。 文檔字符串中的附加說明。
import pandas as pd
import geopandas as gpd # if not needed, remove gpd.GeoDataFrame from the type hinting and no need to import Union
from typing import Union
def glance(df: Union[pd.DataFrame, gpd.GeoDataFrame], size: int = 2) -> None:
""" Provides a shortened head and tail summary of a Dataframe or GeoDataFrame in Jupyter Notebook or Lab.
Usage
----------
# default glance (2 head rows, 2 tail rows)
glance( df )
# glance defined number of rows in head and tail (3 head rows, 3 tails rows)
glance( df, size=3 )
Parameters
----------
:param df: Union[pd.DataFrame, gpd.GeoDataFrame]: A (Geo)Pandas data frame to glance at.
:param size: int: The number of rows in the head and tail to display, total rows will be double provided size.
:return: None: Displays result in Notebook or Lab.
"""
# min and max of the provided dataframe index
min_ = df.index.min()
max_ = df.index.max()
# define slice
sample = [i for i in range(min_, size)] + [i for i in range(max_ - size, max_)]
# slice
df = df[df.index.isin(sample)]
# display
display( df )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.