以下に以前教えていただいたプログラムを示します.
説明はこのURLの通りです.(https://teratail.com/questions/197955)
今回はこの条件を3つに,抜き出す行を4つにしたいです.
以下のように変更すればできると思ったのですができませんでした,
どのようにすれば理想のようにいくのでしょうか.
Python
1#変更したもの 2import pandas as pd 3import io 4 5 6data = """ 71,100,0.145 82,110,0.146 93,120,0.147 104,130,0.148 115,140,0.149 126,150,0.150 137,145,0.150 148,140,0.149 159,130,0.150 1610,120,0.151 1711,150,0.160 1812,140,0.159 1913,130,0.160 2014,120,0.161 21""" 22 23df = pd.read_csv(io.StringIO(data), header=None) 24 25 26target_dfs = [] 27count = 0 28for idx,row in df.iterrows(): 29 if (count == 0) and (row[2] != 0.146) and (row[2] != 0.150) and (row[2] != 0.160): 30 continue 31 elif count == 0: 32 p = df.index.get_loc(idx) 33 target_dfs.append(df.iloc[p:p+4].reset_index(drop=True)) 34 count = (count + 1) if (count < 4) else 0 35 36target = pd.concat(target_dfs, axis=1) 37 38print(target)
Python
1#結果 2 0 1 2 0 1 2 0 1 2 30 2 110 0.146 7 145 0.150 13.0 130.0 0.160 41 3 120 0.147 8 140 0.149 14.0 120.0 0.161 52 4 130 0.148 9 130 0.150 NaN NaN NaN 63 5 140 0.149 10 120 0.151 NaN NaN NaN
Python
1#理想の結果 2 0 1 2 0 1 2 0 1 2 30 2 110 0.146 6 150 0.150 11 150 0.160 41 3 120 0.147 7 145 0.150 12 140 0.159 52 4 130 0.148 8 140 0.149 13 130 0.160 63 5 140 0.149 9 130 0.150 14 120 0.161