Python熊猫-比较特定行并合并
我有以下数据集:Python熊猫-比较特定行并合并,python,pandas,rows,Python,Pandas,Rows,我有以下数据集: Group LowerTier UpperTier Value 1 0 99 0 1 100 199 0 1 200 299 10 1 300 1000 20 2 0 249 0 2
Group LowerTier UpperTier Value
1 0 99 0
1 100 199 0
1 200 299 10
1 300 1000 20
2 0 249 0
2 250 1000 5
(...)
我希望通过Python和pandas获得以下内容:
Group LowerTier UpperTier Value
1 0 199 0
1 200 299 10
1 300 1000 20
2 0 249 0
2 250 1000 5
换句话说:我希望在获得双精度值的下限最小值和上限最大值的条件下(例如,组1;值=0),组合组中具有相同值的所有行
我怎样才能拿到这张桌子
提前感谢您是否尝试使用
聚合
进行groupby
df.groupby(['Group','Value'],as_index=False).agg({'LowerTier':min,'UpperTier':max})
首先创建数据帧:
df = pd.DataFrame({'Group':[1,1,1,1,2,2],'LowerTier':[0,100,200,300,0,250],'UpperTier':[99,199,299,1000,249,1000],'Value':[0,0,10,20,0,5]})
然后使用agg
应用groupby
:
df.groupby(['Group','Value'],as_index=False).agg({'LowerTier':min,'UpperTier':max})
输出:
Group Value UpperTier LowerTier
0 1 0 199 0
1 1 10 299 200
2 1 20 1000 300
3 2 0 249 0
4 2 5 1000 250
您是否尝试使用
aggregate
进行groupby
df.groupby(['Group','Value'],as_index=False).agg({'LowerTier':min,'UpperTier':max})
首先创建数据帧:
df = pd.DataFrame({'Group':[1,1,1,1,2,2],'LowerTier':[0,100,200,300,0,250],'UpperTier':[99,199,299,1000,249,1000],'Value':[0,0,10,20,0,5]})
然后使用agg
应用groupby
:
df.groupby(['Group','Value'],as_index=False).agg({'LowerTier':min,'UpperTier':max})
输出:
Group Value UpperTier LowerTier
0 1 0 199 0
1 1 10 299 200
2 1 20 1000 300
3 2 0 249 0
4 2 5 1000 250
非常感谢你!我使用了透视表,但无法获得正确的数据帧。不客气,祝熊猫好运,它非常强大,值得学习!非常感谢你!我使用了透视表,但无法获得正确的数据帧。不客气,祝熊猫好运,它非常强大,值得学习!