熊猫dataframe返回行,状态,城市和日期多次发生

发布于 2025-01-31 12:12:50 字数 307 浏览 3 评论 0原文

首先,这是我的第一篇文章,所以如果格式差,我很抱歉。

因此,我拥有了我已附加图片的数据框架。它包含不明飞行物的目击事件,我想返回行if城市和状态相同的行,然后如果日期相同。我试图发现在同一城市和州同一天发生的目击事件。如果需要更多信息,请让我知道。 先感谢您!

Firstly, this is my first post, so my apologies if it is formatted poorly.

So I have this dataframe which I have attached a picture of. It contains UFO sightings and I want to return the rows where the if the city and state are the same and then also if the dates are the same. I am trying to find sightings that occurred on the same day in the same city and state. Please let me know if more info is required.
Thank you in advance!

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忱杏 2025-02-07 12:12:50

替代方案,布尔索引以保持 行:

df['date'] = pd.to_datetime(df['occurred_date_time']).dt.normalize()

df2 = df[df.duplicated(['date','city','state'], keep=False)]

如果您不想要新列:

df2 = df[df.assign(date=pd.to_datetime(df['occurred_date_time'])
                          .dt.normalize())
           .duplicated(['date','city','state'], keep=False)]

Alternative, boolean indexing to keep the duplicated rows:

df['date'] = pd.to_datetime(df['occurred_date_time']).dt.normalize()

df2 = df[df.duplicated(['date','city','state'], keep=False)]

If you don't want the new column:

df2 = df[df.assign(date=pd.to_datetime(df['occurred_date_time'])
                          .dt.normalize())
           .duplicated(['date','city','state'], keep=False)]
无人问我粥可暖 2025-02-07 12:12:50

尝试一下。

# Create a column converting date_time to just date
df['date'] = pd.to_datetime(df['occurred_date_time']).dt.normalize()

# groupby and count times where date, city and state 
# then create boolean series where count is greater than 1
m = df.groupby(['date','city','state']).transform("count") > 1

# boolean filter the dataframe rows with that series, m.
df[m]

Try this.

# Create a column converting date_time to just date
df['date'] = pd.to_datetime(df['occurred_date_time']).dt.normalize()

# groupby and count times where date, city and state 
# then create boolean series where count is greater than 1
m = df.groupby(['date','city','state']).transform("count") > 1

# boolean filter the dataframe rows with that series, m.
df[m]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文