如何将新专栏添加到Pandas组?熊猫忘记了专栏

发布于 2025-01-26 19:28:21 字数 1121 浏览 1 评论 0原文

我有一个由PID(参与者标识符)分组的创建at索引的pandas数据框架。列创建的是Unix时间戳。现在,我想在每个组中添加一个新的列,并具有每日和月份的字符串表示形式,以便我可以每天再次分组。

但是熊猫似乎忘记了我添加的新专栏吗?

所以我有这个:

def to_daymonth(timestamp: int):
    datime_obj = datetime.fromtimestamp(timestamp)
    return datime_obj.strftime('%d %b')

for pid, group in bypid:
    group['date'] = group.index.map(to_daymonth)
    print(group.date)  # Inside the for loop this prints the new column like 01 May etc.

# But outside of the for loop
 
print(bypid.get_group('12')['date']) # KeyError: 'date'
print(bypid.get_group('12').date) # AttributeError: 'DataFrame' object has no attribute 'date'

在我看来,熊猫似乎忘记了我添加了日期列吗?我在这里想念什么?

这就是我想记住日期列后想做的。

for pid, group in bypid:
    plt.figure()
    plt.title(pid)
    plt.plot((0,1), (0.5, 0.5))  # Lines for x and y in the middle
    plt.plot((0.5, 0.5), (0, 1))
    for date, dategroup in group.groupby('date'):
        plt.scatter(dategroup.euro, dategroup.dollar, label=date)
    plt.legend(loc='best')
    plt.show()

I have a pandas dataframe indexed by createdAt grouped by pid (participant identifiers). The created at column are unix timestamps. Now I would like to add a new column to each group with a string representation of the day and month so so that I can group again by day.

But pandas seems to forget about the new column I have added?

So I have this:

def to_daymonth(timestamp: int):
    datime_obj = datetime.fromtimestamp(timestamp)
    return datime_obj.strftime('%d %b')

for pid, group in bypid:
    group['date'] = group.index.map(to_daymonth)
    print(group.date)  # Inside the for loop this prints the new column like 01 May etc.

# But outside of the for loop
 
print(bypid.get_group('12')['date']) # KeyError: 'date'
print(bypid.get_group('12').date) # AttributeError: 'DataFrame' object has no attribute 'date'

It seems to me that pandas is forgetting I added the date column? What am I missing here?

This is what I would like to do after it remembers the date column.

for pid, group in bypid:
    plt.figure()
    plt.title(pid)
    plt.plot((0,1), (0.5, 0.5))  # Lines for x and y in the middle
    plt.plot((0.5, 0.5), (0, 1))
    for date, dategroup in group.groupby('date'):
        plt.scatter(dategroup.euro, dategroup.dollar, label=date)
    plt.legend(loc='best')
    plt.show()

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文