简体   繁体   English

Python:Pandas 数据框中的行到列

[英]Python: Rows to Column in Pandas Dataframe

I have following dataframe:我有以下数据框:

data.head()

Out:出去:

     metric_name            metric_date     warehouse   value          week  year   day
0   Crossdock Transfer Out  2022-05-10      WR1        1.370313e+06     19  2022    2
21  New Vendor Freight      2022-04-19      WR1        1.583337e+06     16  2022    2
59  Crossdock Transfer Out  2021-12-26      WR1        3.805000e+03     51  2021    0
80  New Vendor Freight      2021-12-30      WR1        2.832327e+06     52  2021    4
90  Crossdock Transfer In   2022-05-22      WR1        0.000000e+00     20  2022    0
...     ...     ...     ...     ...     ...     ...     ...
127699  LF Forecasted New Crossdock Transfer Out    2021-11-01  WR1     2.595843e+06    44  2021    1

There are different metrics at column "metric_name" with its own value. “metric_name”列有不同的指标,有自己的值。 To have a better dataset for time series forecasting I want to convert my Dataframe.为了获得更好的时间序列预测数据集,我想转换我的 Dataframe。 I want every single metric in "metric_name" as new column in the dataset.我希望“metric_name”中的每个指标都作为数据集中的新列。

See here all different metrics in "metric_name":在此处查看“metric_name”中的所有不同指标:

data.metric_name.value_counts()

Out:出去:

LF Forecasted End Vendor Freight + End Transfer In Backlog    364
LF Forecasted New Crossdock Transfer Out                      364
LF Forecasted New Vendor Freight                              364
LF Forecasted New Crossdock Transfer In                       364
Forecasted New Crossdock Transfer Out                         359
Forecasted End Vendor Freight + End Transfer In Backlog       359
Forecasted New Crossdock Transfer In                          359
Forecasted New Vendor Freight                                 359
Crossdock Transfer Out                                        345
New Vendor Freight                                            345
Crossdock Transfer In                                         345
End Vendor Freight + End Transfer In Backlog                  345
Name: metric_name, dtype: int64

What can I do to solve this?我能做些什么来解决这个问题?

I tried this, but it is not giving me the values, just the counts:我试过这个,但它没有给我价值,只是计数:

bhx4.groupby('metric_date')['metric_name'].value_counts().unstack()

metric_name     Crossdock Transfer In   Crossdock Transfer Out  End Vendor Freight + End Transfer In Backlog    Forecasted End Vendor Freight + End Transfer In Backlog     Forecasted New Crossdock Transfer In    Forecasted New Crossdock Transfer Out   Forecasted New Vendor Freight   LF Forecasted End Vendor Freight + End Transfer In Backlog  LF Forecasted New Crossdock Transfer In     LF Forecasted New Crossdock Transfer Out    LF Forecasted New Vendor Freight    New Vendor Freight
 metric_date                                                
 2021-07-25     1.0     1.0     1.0     1.0     1.0     1.0     1.0         1.0     1.0     1.0     1.0     1.0
 2021-07-26     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0
 2021-07-27     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0
 2021-07-28     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0
 2021-07-29     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0     1.0

I want the DF to look something like this:我希望 DF 看起来像这样: 在此处输入图像描述 Thanks!谢谢!

如果我说对了:

data.groupby(['metric_date', 'metric_name'])['value'].sum().unstack().fillna(0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python pandas Dataframe 列到行操作 - Python pandas Dataframe column to rows manipulation Python Dataframe Pandas - 过滤 Z6A8064B5DF4794555500553C47C55055DZ 行的条件子集 - Python Dataframe Pandas - Filter dataframe rows by condition issubset() on column values 从python pandas数据框中的前几行中减去某列的行 - Subtracting the rows of a column from the preceding rows in a python pandas dataframe Python Pandas:获取DataFrame的行,其中列不为null - Python Pandas: get rows of a DataFrame where a column is not null Python:过滤pandas数据帧以保持基于列的指定行数 - Python: filter pandas dataframe to keep specified number of rows based on a column 遍历一列并填充fucntion Pandas Dataframe Python中的行 - Loop over one column and fill rows in fucntion Pandas Dataframe Python Python Pandas Dataframe按Timedelta列值删除行 - Python Pandas Dataframe Remove Rows by Timedelta Column Value 如何将同一列中的行与 pandas dataframe - python 中的类别相加 - How to sum rows in the same column than the category in pandas dataframe - python Python Pandas:在 DataFrame 的特定列中按模式(跨行)拆分 - Python Pandas: Split by pattern (across rows) in a specific column of DataFrame Python,Pandas; 按列中最常见的值对 Dataframe 行进行排序 - Python, Pandas; Sort Dataframe rows by most frequent values in a column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM