teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

補足を追加

2019/11/10 23:28

投稿

magichan
magichan

スコア15898

answer CHANGED
@@ -44,4 +44,25 @@
44
44
  df = df.drop(drop_index)
45
45
  df.to_csv(f"list1_{(i+1)*0.5:.1f}f.csv")
46
46
  limit -= drop_size
47
+ ```
48
+
49
+ ---
50
+ **【追記2】**
51
+ だいたい、こんな感じになるのではないでしょうか(動作は全く未検証)
52
+ ```Python
53
+ import pandas as pd
54
+ import numpy as np
55
+
56
+ for list_no in range(1,3):
57
+ df = pd.read_csv(f"list{list_no}.csv")
58
+ label = pd.cut(df['time2'], bins=np.arange(0, 10.5, 0.5), right=False, labels=False)
59
+
60
+ limit = 5
61
+ for i in range(0,20):
62
+ drop_size = min((label == i).sum(), limit)
63
+ drop_index = df.loc[label==i,'time2'].sort_values().index[:drop_size]
64
+ df = df.drop(drop_index)
65
+ new_file = f"list{list_no}_{(i+1)*0.5:.1f}f.csv"
66
+ df.to_csv(new_file)
67
+ limit -= drop_size
47
68
  ```

1

サンプルを追加

2019/11/10 23:28

投稿

magichan
magichan

スコア15898

answer CHANGED
@@ -31,4 +31,17 @@
31
31
  df = df.drop(drop_target)
32
32
  df.to_csv(f"list1_{(i+1)*0.5:.1f}f.csv")
33
33
  ```
34
- のようにtime2の値でソートして頭からn個のIndex値を対象とすると良いのではないでしょうか。
34
+ のようにtime2の値でソートして頭からn個のIndex値を対象とすると良いのではないでしょうか。
35
+
36
+ ---
37
+ **追記**
38
+ 削除する行数を全体で制限する場合のサンプル
39
+ ```
40
+ limit = 5
41
+ for i in range(0,20):
42
+ drop_size = min((label == i).sum(), limit)
43
+ drop_index = df.loc[label==i,'time2'].sort_values().index[:drop_size]
44
+ df = df.drop(drop_index)
45
+ df.to_csv(f"list1_{(i+1)*0.5:.1f}f.csv")
46
+ limit -= drop_size
47
+ ```