やりたいことを簡単にしました
python
1df = pd.read_table(io.StringIO(""" 2top row 31 2 42 2 53 0 64 0 75 1 86 1 9"""))
クラスタリングするとこういう結果がでたのですが
top row
1 2
2 2
3 0
4 0
5 1
6 1
こういうtop順にrowを0から番号を振りなおしたい
top row
1 0
2 0
3 1
4 1
5 2
6 2
よろしくお願いします。
以下サンプルは不要です。
PDFの表からテキスト抽出してTSVの表を作成したいのですが
- popplerのpdftohtmlからxmlを作成
- xmlからX/Y座標とテキストのcsvを作成
- X座標とY座標をクラスタリングしクラス分け
クラスタリングはできたのですが順番がバラバラなのでX座標の順、Y座標の順で番号を振り直したい。
df2_results['top'] Y座標
df2_results['left'] X座標
df2_results['row'] Yのクラス
df2_results['col'] Xのクラス
になっています。
よろしくお願いします。
python
1import pandas as pd 2from sklearn.cluster import KMeans 3 4df = pd.read_csv('data.csv') 5 6# ページ・縦・横の順にソート 7df.sort_values(by=['page', 'top', 'left'], inplace=True) 8df.head() 9 10df1 = df.loc[:, ['page', 'top', 'left', 'text']] 11df1.plot.scatter(x='left', y='top', ylim=(892, 0))
python
1# ヘッダー部を除外 2df2 = df1[df1['top'] > 100] 3df2 4 5# 表部分確認 6df2.plot.scatter(x='left', y='top', ylim=(892, 0)) 7 8# Y座標をクラスタリング 9mdl_y = KMeans(n_clusters=33) 10df2_y = df2[['top']] 11mdl_y.fit(df2_y) 12y2 = mdl_y.labels_ 13print(y2) 14 15# X座標をクラスタリング 16mdl_x = KMeans(n_clusters=6) 17df2_x = df2[['left']] 18mdl_x.fit(df2_x) 19x2 = mdl_x.labels_ 20print(x2) 21 22df2_results = df2.copy() 23 24df2_results['row'] = y2 25df2_results['col'] = x2 26 27df2_results.sort_values('top')
python
1# Y座標散布図 2df2_results.plot.scatter( 3 x='left', y='top', c='row', cmap='winter', ylim=(892, 0))
python
1# X座標散布図 2df2_results.plot.scatter( 3 x='left', y='top', c='col', cmap='winter', ylim=(892, 0))
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/15 12:31
2018/06/15 13:55
2018/06/15 13:58
2018/06/15 14:50 編集
2018/06/15 14:58
2018/06/15 15:27