我有一个巨大的EXCEL表格,我使用的是Python.一个例子:

Date id_m00 mprice id_m01 mprice
01.01.2023 aa-bb-cc 12,05 dd-ee-fr 8,80
02.01.2023 aa-dd-ee 09,55 ff-gg-gg 7,50

这个列模式遵循的是它的46倍.如id_m02和mrpice;id_q1和mPrice...

我想要的是:

Date id mprice
01.01.2023 aa-bb-cc 12,05
02.01.2023 aa-dd-ee 09,55
01.01.2023 dd-ee-fr 8,80
02.01.2023 ff-gg-gg 7,50

你知道怎么用Python语言做到这一点吗?我(第一次)使用了熔化功能,但做得不好.它以一些额外的列和大量的空值结束.

推荐答案

可能的解决方案有lreshape个:

prices = (df.pop("mprice").pipe(lambda x:
      x.set_axis(range(len(x.columns)), axis=1)))

out = (
    pd.lreshape(
        pd.concat([df, prices], axis=1),
        {"id": df.filter(like="id_m").columns, "mprice": prices.columns})
)

NB : The code above can be simplified if the example you shared correspond to the actual table in the spreadsheet. If so, while making the initial DataFrame, pandas will make sure to de-duplicate the 100 and will give 101, 102, .. 103:

out = (
    pd.lreshape((df:=pd.read_excel("file.xlsx")), # << feel free to adjust
        {"id": df.filter(like="id_m").columns,
         "mprice": df.filter(like="price").columns})
)

发帖主题:Re:Kolibrios

print(out)

         Date        id  mprice
0  01.01.2023  aa-bb-cc   12.05
1  02.01.2023  aa-dd-ee    9.55
2  01.01.2023  dd-ee-fr    8.80
3  02.01.2023  ff-gg-gg    7.50

Python相关问答推荐

如何将 map 数组组合到pyspark中每列的单个 map 中

按 struct 值对Polars列表[struct[]]排序

有没有方法可以修复删除了换码字符的无效的SON记录?

如何在矩阵上并行化简单循环?

Twilio:CallInstance对象没有来自_的属性'

使用polars .滤镜进行切片速度比pandas .loc慢

使用FASTCGI在IIS上运行Django频道

ModuleNotFound错误:没有名为flags.State的模块; flags不是包

如何找到满足各组口罩条件的第一行?

在Python中管理打开对话框

删除字符串中第一次出现单词后的所有内容

NumPy中条件嵌套for循环的向量化

如何更新pandas DataFrame上列标题的de值?

如何指定列数据类型

如何使用使用来自其他列的值的公式更新一个rabrame列?

从列表中获取n个元素,其中list [i][0]== value''

替换现有列名中的字符,而不创建新列

基于Scipy插值法的三次样条系数

提高算法效率的策略?

如何强制向量中的特定元素在Gekko中处于优化解决方案中