confirmed = confirmed_data.groupby('Country/Region').agg({dt: 'sum'}).to_dict()[dt]
时间: 2023-08-22 21:03:39 浏览: 129
这是一行使用pandas库的代码,做了以下几件事情:
1. 针对一个叫做confirmed_data的数据集,按照国家/地区进行分组。
2. 对每一个分组,计算指定的时间(dt)的总和。
3. 将结果转换为一个字典,其中键是国家/地区名称,值是指定时间的总和。
其中,dt是一个代表时间的变量,具体取决于在该代码块前面是否有定义。
相关问题
import pandas as pd import matplotlib.pyplot as plot raw_data = pd.read_csv(r'/Users/wangruihan/Desktop/time_series_covid19_confirmed_global.csv',na_values='?') #设定'?'为缺失值 #查看有哪些列 print(raw_data.columns) #查看前几行数据 print(raw_data[:10]) #检查含有缺失值的列 print(raw_data.isnull().any() ) print(raw_data.shape) #缺失值处理 print(raw_data[raw_data.isnull().values==True]) fill_na = lambda col:col.fillna('not know')#定义fill_na函数,用'not know'字符串填充缺失值 fill_data = raw_data.apply(fill_na, axis=0) #将填充后的数据赋给fill_data print(fill_data.isnull().any()) #检查是否填充成功 isDuplicated=fill_data.duplicated() #判断重复数据记录 print(isDuplicated) #初步了解数据构成 print(fill_data[fill_data['Country/Region']=='China']) data = fill_data.drop(['Province/State','Lat','Long'],axis=1) #删除属性列 data = data.groupby('Country/Region').sum() # 按Country/Region并求和 print(data.index.values) #列出全部可索引的Country/Region名称 data_us = data[data.index.isin(['US','China','United Kingdom','Italy','Germany','France'])] print(data_us) data_us.T.plot(figsize=(15,7)) #数据趋势图 最后数据趋势图不显示怎么办
如果你在Jupyter Notebook中运行这段代码并且没有看到数据趋势图,则可以在代码的最后加上`plt.show()`命令,如下所示:
```
data_us.T.plot(figsize=(15,7))
plt.show()
```
这个命令将显示图表并将其添加到Jupyter Notebook中。请注意,你需要首先导入matplotlib库,使用`import matplotlib.pyplot as plt`命令。
def delete_selected(modeladmin, request, queryset): """ Default action which deletes the selected objects. This action first displays a confirmation page which shows all the deletable objects, or, if the user has no permission one of the related childs (foreignkeys), a "permission denied" message. Next, it deletes all selected objects and redirects back to the change list. """ opts = modeladmin.model._meta app_label = opts.app_label # Check that the user has delete permission for the actual model if not modeladmin.has_delete_permission(request): raise PermissionDenied using = router.db_for_write(modeladmin.model) # Populate deletable_objects, a data structure of all related objects that # will also be deleted. deletable_objects, model_count, perms_needed, protected = get_deleted_objects( queryset, opts, request.user, modeladmin.admin_site, using) # The user has already confirmed the deletion. # Do the deletion and return a None to display the change list view again. if request.POST.get('post') and not protected: if perms_needed: raise PermissionDenied n = queryset.count() if n: for obj in queryset: obj_display = force_text(obj) modeladmin.log_deletion(request, obj, obj_display) queryset.delete() modeladmin.message_user(request, _("Successfully deleted %(count)d %(items)s.") % { "count": n, "items": model_ngettext(modeladmin.opts, n) }, messages.SUCCESS) # Return None to display the change list page again. return None if len(queryset) == 1: objects_name = force_text(opts.verbose_name) else: objects_name = force_text(opts.verbose_name_plural) if perms_needed or protected: title = _("Cannot delete %(name)s") % {"name": objects_name} else: title = _("Are you sure?") context = dict( modeladmin.admin_site.each_context(request), title=title, objects_name=objects_name, deletable_objects=[deletable_objects], model_count=dict(model_count).items(), queryset=queryset, perms_lacking=perms_needed, protected=protected, opts=opts, action_checkbox_name=helpers.ACTION_CHECKBOX_NAME, media=modeladmin.media, ) request.current_app = modeladmin.admin_site.name # Display the confirmation page return TemplateResponse(request, modeladmin.delete_selected_confirmation_template or [ "admin/%s/%s/delete_selected_confirmation.html" % (app_label, opts.model_name), "admin/%s/delete_selected_confirmation.html" % app_label, "admin/delete_selected_confirmation.html" ], context) delete_selected.short_description = ugettext_lazy("Delete selected %(verbose_name_plural)s")
这段代码定义了一个默认的 Django Admin 后台操作,用于批量删除选中的对象。具体来说,这个操作会首先显示一个确认页面,列出所有将被删除的对象,以及与这些对象相关的其他对象(例如外键关联的对象)。如果用户没有删除权限,或者某些对象受到保护,则会显示一个“无权删除”或“不能删除”的消息。如果用户确认删除操作,则会删除所有选中的对象,并重定向到对象列表页面。
在代码中,`delete_selected()` 函数接受三个参数:`modeladmin` 表示 Django admin 后台的管理器对象,`request` 表示当前的 HTTP 请求对象,`queryset` 表示选中的对象的 QuerySet。函数首先获取模型的元数据信息,然后检查当前用户是否有删除权限。如果没有权限,则会引发 `PermissionDenied` 异常。接下来,函数调用 `get_deleted_objects()` 方法获取所有将被删除的对象及相关对象的信息,并根据用户的选择执行删除操作或者显示确认页面。最后,函数返回一个 `TemplateResponse` 对象,用于显示确认页面或者执行删除操作后的重定向。此外,函数还定义了一个 `short_description` 属性,用于在 Django admin 后台中显示操作名称。
阅读全文