Python 3.x 数据帧分组方式,并在新数据帧中查找值
我有一个数据框,我必须看看是否存在一个条目,其中每个市场都有上周六的数据,这也是一个最大数据条目Python 3.x 数据帧分组方式,并在新数据帧中查找值,python-3.x,pandas,pandas-groupby,Python 3.x,Pandas,Pandas Groupby,我有一个数据框,我必须看看是否存在一个条目,其中每个市场都有上周六的数据,这也是一个最大数据条目 data = { 'marketplace': [3, 3, 4, 4, 5, 3, 4], 'date': ['2017-11-11', '2017-11-10', '2017-11-07', '2017-11-08', '2017-11-10', '2017-11-09', '2017-11-10'] } last_saturday = '2017-
data = {
'marketplace': [3, 3, 4, 4, 5, 3, 4],
'date': ['2017-11-11', '2017-11-10', '2017-11-07', '2017-11-08', '2017-11-10', '2017-11-09', '2017-11-10']
}
last_saturday = '2017-11-11'
df = pd.DataFrame(data, columns= ['marketplace', 'date'])
df_sub = df.groupby(['marketplace'])['date'].max()
print(df_sub)
我得到df\u sub=
marketplace
3 2017-11-11
4 2017-11-10
5 2017-11-10
Name: date, dtype: object
我如何反复查看df_sub
以查看市场的日期是否与上周六
匹配
当我试图打印日期时,我得到以下错误:
TypeError: an integer is required
tz=getattr(series.dtype, 'tz', None))
File "pandas/_libs/index.pyx", line 83, in pandas._libs.index.IndexEngine.get_value
File "pandas/_libs/index.pyx", line 91, in pandas._libs.index.IndexEngine.get_value
File "pandas/_libs/index.pyx", line 141, in pandas._libs.index.IndexEngine.get_loc
KeyError: 'date'
我假设为了访问
dfu sub
中的数据,我必须使用iloc
或loc
,但不确定如何使用 我相信您需要仅将系列
与值进行比较-获取布尔掩码,并需要检查至少一个True
:
print ((df_sub == last_saturday).any())
True
print (df_sub == last_saturday)
3 True
4 False
5 False
Name: date, dtype: bool
或者先通过参数
创建DataFrame
作为_index=False
或:
和比较列:
print ((df_sub['date'] == last_saturday).any())
True
我相信您需要仅将
系列
与值进行比较-获取布尔掩码,并需要至少检查一个真
:
print ((df_sub == last_saturday).any())
True
print (df_sub == last_saturday)
3 True
4 False
5 False
Name: date, dtype: bool
或者先通过参数
创建DataFrame
作为_index=False
或:
和比较列:
print ((df_sub['date'] == last_saturday).any())
True