python - 如何在python中为数据框的每个类别创建列
问题描述
我在数据框中有一列,它有 6 个分类变量。我想为每个类别创建 1 列。到目前为止,我一直在使用 pivot_table,但它似乎不是一个正确的答案,因为我不想使用任何 aggfunc 并且只想返回具有相同对应值的数据帧。下面在我的数据框示例中:
Date Region Total confirmed deaths (deaths)
6452 2020-03-03 Asia 0
10514 2020-01-02 Asia 0
1416 2020-01-21 Europe 0
9595 2020-02-06 Asia 1
12221 2020-01-07 Europe 0
12828 2020-01-14 Rest of the world 1
6232 2020-03-07 Europe 197
10812 2020-01-05 Rest of America 0
11984 2020-04-18 Rest of the world 37
6572 2020-03-28 Rest of the world 1
1297 2020-03-19 Asia 1
4851 2020-03-20 Rest of the world 0
下面是我用于数据透视表的代码:
pivoted = df.pivot_table(index='Date', columns='Region', values='Total confirmed deaths (deaths)')
pivoted.resample('W')
解决方案
推荐阅读
- java - Java:睡眠或等到特定时间
- responsive - 为什么 youtube 和 vimeo 之间的响应式视频高度不同
- variables - SAS如何在建模前准备变量
- c - 我无法使用 malloc() 和 realloc() 函数分配内存
- javascript - 如何使用 history.pushState 更改 url 以使用 jQuery Ajax 创建页面转换?
- python - 如何拆分像 'A[B][q]G[boy]' 这样的 SGF 键/值对?
- react-native - react-native-elements 的表单组件导致“不变违规”错误
- angular - 如何在Angular中检查此数据中是否存在用户名?
- express - 使用 JWT——在服务器端有没有办法检查用户当前是否登录?
- c++ - 在 C++ 中编码解码的 url