请问id相同的行超过50行,如果缩减为25行?

请问id相同的行超过50行,如果缩减为25行?python想用 if item["New_ID"] %2==0 的方式取奇数或者偶数,请问应该怎么写呀
请问id相同的行超过50行,如果缩减为25行?

newDf1 = pd.DataFrame(columns=['Time', 'eleCur',  'id'])

for key, item in df1.groupby(['id']):

if item.shape[0] > 20 :

if item["New_ID"] %2==0:

item['id'] = key

newDf1 = pd.concat([newDf1, item])

print(key)

print(item)


回答:

newDf1.insert(0, 'New_ID', range(1, 1 + len(newDf1)))
newDf2= pd.DataFrame(columns=['Time', 'eleCur', 'id'])
for key, item in newDf1.groupby(['id']):

item['id'] = key

if item.shape[0] > 4000:

item = item[item["New_ID"] % 20 == 0]

elif item.shape[0] > 3000 :

item = item[item["New_ID"] % 15 == 0]

elif item.shape[0] > 2500:

item = item[item["New_ID"] % 12 == 0]

elif item.shape[0] > 2000 :

item = item[item["New_ID"] % 10 == 0]

elif item.shape[0] > 1000 :

item = item[item["New_ID"] % 8 == 0]

elif item.shape[0] > 500:

item = item[item["New_ID"] % 5 == 0]

elif item.shape[0] > 50 :

item = item[item["New_ID"] % 2 == 0]

else:

item = item

newDf2 = pd.concat([newDf2, item])

以上是 请问id相同的行超过50行,如果缩减为25行? 的全部内容, 来源链接: utcz.com/p/938638.html

回到顶部