以下のようなdf(ツイッターデータ)をgeojsonのような辞書型のデータに変換したいです
実際はcsvファイルを読み込んでいますが、今回は自身でdfを作成します
df = pd.DataFrame({ 'tid' : [1, 2, 3, 4, 5],
'userid' : [1, 1, 2, 3, 3],
'latitude' : [1.1, 2.2, 9.7, 9.8, 9.9],
'longitude' : [11.1, 22.2, 99.9, 99.8, 99.9],
'date' : ['2017-01-01', '2017-01-05', '2017-01-02', '2017-01-07', '2017-01-09',],
'text' : ['a', 'b', 'c', 'd', 'e'],
'label': [1, 2, 3, 3, 3] })
緯度経度が近い箇所で呟かれたツイートを判別して、上記のようなlabelを付与しました
上記のdfのtidが5,6,7のものは近くで呟かれているため同じlabelを付与しています
上記のようなdfを以下のような辞書型やリストが混合したのデータに変換したいです
{'crs': {'properties': {'name': 'EPSG:4326'}, 'type': 'name'},
'features': [
{'geometry': {'coordinates': [[[11.1, 1.1]]],'type': 'Polygon'},
'properties': {'color': 'rgba(0, 0, 255, 0.1)',
'tweet': {'date': ['2017-01-01'],
'label': '1',
'user': {'id': ['1'],
'text': ['a']}}},
'type': 'Feature'},
{'geometry': {'coordinates': [[[22.2, 2.2]]],'type': 'Polygon'},
'properties': {'color': 'rgba(0, 0, 255, 0.1)',
'tweet': {'date': ['2017-01-05'],
'label': '2',
'user': {'id': ['1'],
'text': ['b']}}},
'type': 'Feature'},
{'geometry': {'coordinates': [[[99.9, 9.7], [99.8, 9.8], [99.9, 9.9]]],'type': 'Polygon'},
'properties': {'color': 'rgba(0, 0, 255, 0.1)',
'tweet': {'date': ['2017-01-02', '2017-01-07','2017-01-09'],
'label': '3',
'user': {'id': ['2', '3', '3'],
'text': ['c', 'd', 'e']}}},
'type': 'Feature'}
],'type': 'FeatureCollection'}
1行目は今回は特に意味がありません(4、11、18行目のcolorも同様)
3行目のgeometryから9行目までが一区切りで、labelで分類しています
そのため今回はgeometryから始まる辞書が3つできています(dfのlabelが3種類あるため)
辞書の中には同じlabel同士の、dfのdateやtextといったデータが格納されています
説明不足かもしれませんが宜しくお願い致します
あなたの回答
tips
プレビュー