[英]Count specific characters of json column in Python
說我有一個數據框df
,如下所示:
id geojson
0 1010020478 {"name": "entities", "type": "FeatureCollectio...
1 1010020478 {"name": "entities", "type": "FeatureCollectio...
2 1010020478 {"name": "entities", "type": "FeatureCollectio...
3 1010020478 {"name": "entities", "type": "FeatureCollectio...
4 1010020478 {"name": "entities", "type": "FeatureCollectio...
5 1010020478 {"name": "entities", "type": "FeatureCollectio...
6 1010020478 {"name": "entities", "type": "FeatureCollectio...
7 1010020478 {"name": "entities", "type": "FeatureCollectio...
8 1010020478 {"name": "entities", "type": "FeatureCollectio...
9 1010020478 {"name": "entities", "type": "FeatureCollectio...
我想計算每一行的"type": "e"
或"type":"e"
數
{
"name": "entities","type": "FeatureCollection","features": [
{
"type": "Feature","geometry": {
"type": "Polygon","coordinates": [
[
[
17.11460135135052,9.38631
],[
15.205521351350516,9.38631
],[
15.205521351350516,16.22718
],[
25.228191351350503,16.22718
],[
25.228191351350503,23.38623
],[
7.728291351350526,23.38623
],[
7.728291351350526,0
],[
17.11460135135052,0
],[
17.11460135135052,9.38631
]
]
]
},"properties": {
"area": "104","name": "201","type": "e","Layer": "0","SubClasses": "AcDbEntity:AcDbBlockReference","EntityHandle": "120A"
}
},{
"type": "Feature","geometry": {
"type": "Polygon","coordinates": [
[
[
33.1826913513505,23.38623
],[
25.228191351350503,23.38623
],[
25.228191351350503,16.22718
],[
33.1826913513505,16.22718
],[
33.1826913513505,23.38623
]
]
]
},"properties": {
"area": "125","name": "202","type": "e","Layer": "0","SubClasses": "AcDbEntity:AcDbBlockReference","EntityHandle": "1223"
}
},{
"type": "Feature","geometry": {
"type": "Polygon","coordinates": [
[
[
25.228191351350503,16.22718
],[
15.205521351350516,16.22718
],[
15.205521351350516,9.38631
],[
25.228191351350503,9.38631
],[
25.228191351350503,16.22718
]
]
]
},"properties": {
"area": " ","name": " ","type": " ","Layer": "0","SubClasses": "AcDbEntity:AcDbBlockReference","EntityHandle": "1232"
}
},{
"type": "Feature","geometry": {
"type": "Polygon","coordinates": [
[
[
33.1826913513505,16.22718
],[
25.228191351350503,16.22718
],[
25.228191351350503,9.38631
],[
26.182731351350505,9.38631
],[
33.1826913513505,9.38631
],[
33.1826913513505,16.22718
]
]
]
},"properties": {
"area": "22","name": " ","type": "p","Layer": "0","SubClasses": "AcDbEntity:AcDbBlockReference","EntityHandle": "123D"
}
},{
"type": "Feature","geometry": {
"type": "Polygon","coordinates": [
[
[
38.1144813513505,14.95446
],[
33.18269135135051,14.95446
],[
33.18269135135051,9.38631
],[
38.1144813513505,9.38631
],[
38.1144813513505,14.95446
]
]
]
},"properties": {
"area": "20","name": " ","type": "t","Layer": "0","SubClasses": "AcDbEntity:AcDbBlockReference","EntityHandle": "1267"
}
}
]
}
我嘗試過:
from collections import Counter
z = [""type": "e"", ""type":"e""]
df.geojson.str(Counter(z))
但是我得到了SyntaxError: invalid syntax
。 請幫我。 謝謝。
這是我的解決方案:
import pandas as pd
import json
def o_count(json_str):
data = json.loads(json_str.replace(",", ","))
try:
result = []
features = data['features']
for x in features:
result.append(x['properties']['type'])
return result.count('o')
except:
return 0
df = pd.read_excel("test.xlsx")
df["num"] = df.apply(lambda row: o_count(row['geojson']), axis=1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.