如何将新专栏添加到Pandas组?熊猫忘记了专栏
我有一个由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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论