熊猫 - 枢轴/堆栈/unstack/熔体
我有一个看起来像这样的数据框:
名称 | 值1 | 值2 |
---|---|---|
A | 100 | 101 |
A | 100 | 102 |
A | 100 | 103 |
B | 200 | 201 |
b | 200 B 200 | 202 |
B | 200 | 203 |
C | 300 | 301 C 300 301 |
C | 300 | 302 |
C | 300 | 303 |
,我正在尝试实现此处:
名称 | 值1 | 值2 | 值3 | 值4 | 值5 | 值6 |
---|---|---|---|---|---|---|
A | 100 | 101 | 100 | 102 | 100 | 103 |
B | 200 | 201 | 200 | 202 | 200 | 203 |
C | 300 | 301 300 | 302 | 300 | 300 | 303 |
这是我到目前为止尝试的; dataframe.stack()
dataframe.unstack()
dataframe.melt(id_vars = ['name'])
我需要通过确保;
- 第一行保持原样,但是与同名相关的每个后续值都应将其转换为coulmn。
- 第二个值b(对于。ex)应将其关联的值作为新值下的新值转置,但它不应完全形成单独的值。
I have a dataframe that looks like this:
name | value 1 | value 2 |
---|---|---|
A | 100 | 101 |
A | 100 | 102 |
A | 100 | 103 |
B | 200 | 201 |
B | 200 | 202 |
B | 200 | 203 |
C | 300 | 301 |
C | 300 | 302 |
C | 300 | 303 |
And I'm trying to get to this:
name | value 1 | value 2 | value 3 | value 4 | value 5 | value 6 |
---|---|---|---|---|---|---|
A | 100 | 101 | 100 | 102 | 100 | 103 |
B | 200 | 201 | 200 | 202 | 200 | 203 |
C | 300 | 301 | 300 | 302 | 300 | 303 |
Here is what i have tried so far;dataframe.stack()
dataframe.unstack()
dataframe.melt(id_vars=['name'])
I need to transpose the data by ensuring that;
- The first row remains as it is but every subsequent value associated with the same name should be transposed to a coulmn.
- Whereas the second value B (for. ex) should transpose it's associated value as a new value under the column A values, it should not form a separate altogether.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试:
打印:
Try:
Prints:
每个名称的平坦值
Flatten values for each name
使用
融化,
groupby`和 pivot_wider (来自 pyjanitor ):One option using
melt,
groupby`, and pivot_wider (from pyjanitor):