I am trying to reshape the dataframe below (imported from a .csv), keeping the Easting, Northing and Node Name value together on the same row, but having everything 'stacked' in 4 columns. So I want data in columns V0e, V0n and Vd on top of data in columns S0_Pe, S0_Pn and S0_Pd. In reality there are 8 sets of these easting/northing/node trios. Would I need to rename V0e, V0n, S0_Pe and S0_Pn to 'Easting' and 'Northing' and Vd & S0_Pd to 'Node'? I have experimented with grouby, stack & melt but either everything ends up to two columns ('shot' and everything else) or fails to group as I want.
I have also looked at MultiIndex, with the Node in a level above the easting/northing pair, but I failed to apply it to the existing df as loaded from my .csv file.

Index   shot    V0e         V0n         Vd   S0_Pe      S0_Pn       S0_Pd
0       1001    530811.1    6764623.3   Vd   nan        nan         S0_Pd
1       1002    530808.8    6764617.4   Vd   530771.3   6764510.4   S0_Pd
2       1003    530806.6    6764611.4   Vd   nan        nan         S0_Pd
3       1004    530804.2    6764605.8   Vd   530765.6   6764499.1   S0_Pd

我不介意它看起来像这样:

Index   shot    V0e         V0n         Vd   
0       1001    530811.1    6764623.3   Vd   
1       1002    530808.8    6764617.4   Vd   
2       1003    530806.6    6764611.4   Vd   
3       1004    530804.2    6764605.8   Vd   
4       1001    nan         nan         S0_Pd
5       1002    530771.3    6764510.4   S0_Pd
6       1003    nan         nan         S0_Pd
7       1004    530765.6    6764499.1   S0_Pd

或者,我只需要坐标对和 node 一起移动:

Index   shot    V0e         V0n         Vd   
0       1001    530811.1    6764623.3   Vd   
1       1001    nan         nan         S0_Pd    
2       1002    530808.8    6764617.4   Vd   
3       1002    530771.3    6764510.4   S0_Pd    
4       1003    530806.6    6764611.4   Vd
5       1003    nan         nan         S0_Pd
6       1004    530804.2    6764605.8   Vd
7       1004    530765.6    6764499.1   S0_Pd

推荐答案

您可以使用经常被遗忘的pd.lreshape来执行此操作:

此函数是pd.wide_to_long的通用版本,其中传递的字典为{new_column name: [*columns to vertically stack]}.然后,此字典中的任何未指定列都将被meltd以适合输出.

import pandas as pd

out = pd.lreshape(
    df, 
    {'V0e': ['V0e', 'S0_Pe'], 
     'V0n': ['V0n', 'S0_Pn'], 
     'Vd': ['Vd', 'S0_Pd']}, 
    dropna=False
)

print(out)
   Index  shot       V0e        V0n     Vd
0      0  1001  530811.1  6764623.3     Vd
1      1  1002  530808.8  6764617.4     Vd
2      2  1003  530806.6  6764611.4     Vd
3      3  1004  530804.2  6764605.8     Vd
4      0  1001       NaN        NaN  S0_Pd
5      1  1002  530771.3  6764510.4  S0_Pd
6      2  1003       NaN        NaN  S0_Pd
7      3  1004  530765.6  6764499.1  S0_Pd

Python相关问答推荐

在Python中添加期货之间的延迟

根据多列和一些条件创建新列

为什么我的(工作)代码(生成交互式情节)在将其放入函数中时不再工作?

customtkinter中使用的这个小部件的名称是什么

Polars Dataframe:如何按组删除交替行?

如何使用bs 4从元素中提取文本

如何终止带有队列的Python进程?+ 队列大小的错误?

从今天起的future 12个月内使用Python迭代

opencv Python稳定的图标识别

使用Ubuntu、Python和Weasyprint的Docker文件-venv的问题

Python -Polars库中的滚动索引?

更改键盘按钮进入'

如何获取TFIDF Transformer中的值?

用NumPy优化a[i] = a[i-1]*b[i] + c[i]的迭代计算

我们可以为Flask模型中的id字段主键设置默认uuid吗

如何使用它?

在含噪声的3D点网格中识别4连通点模式

将标签移动到matplotlib饼图中楔形块的开始处

在Admin中显示从ManyToMany通过模型的筛选结果

将一个双框爆炸到另一个双框的范围内