如何将数据帧拆分为两行,其中两列具有特定的值?

2024-05-18 05:14:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个按列包含信息的数据帧,例如:

Month  Year     Cost_1 Cost_2
1      2017     100    0
2      2017     0      100
3      2017     140    30

我希望将这些数据转换成如下形式:

Month  Year     Cost_1 Cost_2 Type
1      2017     100    0      Cost_1
2      2017     0      100    Cost_2
3      2017     140    0      Cost_1
3      2017     0      30     Cost_2

我最初的想法是使用.loc(Cost\u 1>;0,“Type”)=“Cost\u 1”,但这不适用于既有Cost\u 1又有Cost\u 2并且需要添加新行的行?我应该先拆分数据,使其只有Cost_1或Cost_2,然后使用.loc来创建类型列,还是有更聪明的方法来实现这一点?你知道吗

编辑:

这个问题实际上比我最初想的要复杂。每列都有一个关联的合作伙伴成本\u 1有计数\u 1,成本\u 2有计数\u 2。。等等

   Year  Month BDADExclIncurred_Capped_count  BDADExclIncurred_Capped_mean  BDTPDIncurred_Capped_count BDTPDIncurred_Capped_mean
0  2015      5                             0                         NaN                          60                         900
1  2015     10                             0                         NaN                           0                         NaN 
2  2015     12                             0                         NaN                           0                         NaN 
3  2016      1                            60                        2000                           0                         NaN 
4  2016      1                           100                        1500                          20                         600 

这就是我的数据以前的样子,许多列被分解成计数:平均对,我想把它们放在一起,但是如果有一排有两个计数:平均对我想把它分成两行,每行只有一个对应的计数:平均对。然后我希望创建一个名为“type”的新列,它告诉我计数:平均对与该行关联的是。你知道吗

   Year  Month BDADExclIncurred_Capped_count  BDADExclIncurred_Capped_mean  BDTPDIncurred_Capped_count BDTPDIncurred_Capped_mean Type
0  2015      5                             0                         NaN                          60                         900  TPD
1  2015     10                             0                         NaN                           0                         NaN  
2  2015     12                             0                         NaN                           0                         NaN  
3  2016      1                            60                        2000                           0                         NaN  AD
4  2016      1                           100                        1500                           0                           0  AD
5  2016      1                             0                           0                          20                         600  TPD

如本例所示,将创建一个新行。上一个数据帧的索引4现在被拆分为索引4和索引5。你知道吗


Tags: 数据typecountnanmeanyearloc计数
1条回答
网友
1楼 · 发布于 2024-05-18 05:14:30

假设只有Cost_1Cost_2大于零,如您的示例所示,下面是一种简单的方法,可以一步用Cost_1Cost_2填充Type

c = ['Cost_1','Cost_2']
counts = df[c].gt(0).dot(df[c].columns + ',').str.rstrip(',').str.split(',')
counts_df = pd.DataFrame(counts.tolist(), columns = ['Count_1', 'Count_2'])
df.assign(**counts_df)

    Month  Year  Cost_1 Count_1  Cost_2 Count_2
0      1  2017     100  Cost_1       0       0
1      2  2017       0  Cost_2     100       0
2      3  2017     140  Cost_1      30  Cost_2

相关问题 更多 >