Python 如何在一瞬间将api调用的修剪输出放入数据帧中?
api返回我需要的此类数据Lat&Long作为DF中的独立列: {“成功”:true,“错误”:false,“数据”:[{“Id”:“JTDKDTB38E1570707”,“名称”:“AE2” 42K,“板”:空,“Lat”:49.2771263,“Lon”:-122.909874,“燃料”: 75,“地址”:“0”},{“Id”:“JTDKDTB36G1138197”,“名称”:“DN706K”, “板”:空,“横向”:49.27695,“纵向”:-122.91011,“燃料”: 85,“地址”:“0”},“Id”:“JTDKDTB39G1117652”,“名称”:“CH633A”, “标牌”:空,“Lat”:49.27719,“Lon”:-122.910339,“燃料”:79,“地址”:“0” “}} 但在通过阅读json之后,我将“success”、“error”和“data”作为三列Python 如何在一瞬间将api调用的修剪输出放入数据帧中?,python,pandas,Python,Pandas,api返回我需要的此类数据Lat&Long作为DF中的独立列: {“成功”:true,“错误”:false,“数据”:[{“Id”:“JTDKDTB38E1570707”,“名称”:“AE2” 42K,“板”:空,“Lat”:49.2771263,“Lon”:-122.909874,“燃料”: 75,“地址”:“0”},{“Id”:“JTDKDTB36G1138197”,“名称”:“DN706K”, “板”:空,“横向”:49.27695,“纵向”:-122.91011,“燃料”: 85,“地址
pd.read_json(requests.get('url').contents)
它有大约1000行,我将不得不处理数据列以获得lat-long
def LatRegex(x):
Lat = re.findall('"Lat":(.{,10}),',str(x))
def LongRegex(x):
Long = re.findall('"Lon":(.{,10}),',str(x))
df['Lat'] = df['data'].apply(LatRegex)
df['Long'] = df['data'].apply(LongRegex)
但是它不起作用&花了太多时间&我需要在一秒钟内得到数据。有什么建议吗???使用
。应用
Ex:
import json
import pandas as pd
data = json.loads('''{"success":true,"error":false,"data":[{"Id":"JTDKDTB38E1570707","Name":"AE2 42K", "Plate" :null, "Lat": 49.2771263,"Lon": -122.909874,"Fuel": 75,"Address": "0 "},{"Id" :"JTDKDTB36G1138197", "Name": "DN706K", "Plate" :null,"Lat" : 49.27695,"Lon": -122.91011,"Fuel": 85,"Address":"0 "},{"Id": "JTDKDTB39G1117652", "Name": "CH633A", "Plate" :null,"Lat":49.27719,"Lon":-122.910339,"Fuel":79,"Address":"0 "}]}''')
df = pd.DataFrame(data)
df["Lat"] = df["data"].apply(lambda x: x["Lat"])
df["Lon"] = df["data"].apply(lambda x: x["Lon"])
df.drop("data", inplace=True, axis=1)
print(df)
error success Lat Lon
0 False True 49.277126 -122.909874
1 False True 49.276950 -122.910110
2 False True 49.277190 -122.910339
import json
from pandas.io.json import json_normalize
data = json.loads('''{"success":true,"error":false,"data":[{"Id":"JTDKDTB38E1570707","Name":"AE2 42K", "Plate" :null, "Lat": 49.2771263,"Lon": -122.909874,"Fuel": 75,"Address": "0 "},{"Id" :"JTDKDTB36G1138197", "Name": "DN706K", "Plate" :null,"Lat" : 49.27695,"Lon": -122.91011,"Fuel": 85,"Address":"0 "},{"Id": "JTDKDTB39G1117652", "Name": "CH633A", "Plate" :null,"Lat":49.27719,"Lon":-122.910339,"Fuel":79,"Address":"0 "}]}''')
df = json_normalize(data, 'data', ['success', 'error'])
print(df)
Address Fuel Id Lat Lon Name Plate \
0 0 75 JTDKDTB38E1570707 49.277126 -122.909874 AE2 42K None
1 0 85 JTDKDTB36G1138197 49.276950 -122.910110 DN706K None
2 0 79 JTDKDTB39G1117652 49.277190 -122.910339 CH633A None
success error
0 True False
1 True False
2 True False
输出:
import json
import pandas as pd
data = json.loads('''{"success":true,"error":false,"data":[{"Id":"JTDKDTB38E1570707","Name":"AE2 42K", "Plate" :null, "Lat": 49.2771263,"Lon": -122.909874,"Fuel": 75,"Address": "0 "},{"Id" :"JTDKDTB36G1138197", "Name": "DN706K", "Plate" :null,"Lat" : 49.27695,"Lon": -122.91011,"Fuel": 85,"Address":"0 "},{"Id": "JTDKDTB39G1117652", "Name": "CH633A", "Plate" :null,"Lat":49.27719,"Lon":-122.910339,"Fuel":79,"Address":"0 "}]}''')
df = pd.DataFrame(data)
df["Lat"] = df["data"].apply(lambda x: x["Lat"])
df["Lon"] = df["data"].apply(lambda x: x["Lon"])
df.drop("data", inplace=True, axis=1)
print(df)
error success Lat Lon
0 False True 49.277126 -122.909874
1 False True 49.276950 -122.910110
2 False True 49.277190 -122.910339
import json
from pandas.io.json import json_normalize
data = json.loads('''{"success":true,"error":false,"data":[{"Id":"JTDKDTB38E1570707","Name":"AE2 42K", "Plate" :null, "Lat": 49.2771263,"Lon": -122.909874,"Fuel": 75,"Address": "0 "},{"Id" :"JTDKDTB36G1138197", "Name": "DN706K", "Plate" :null,"Lat" : 49.27695,"Lon": -122.91011,"Fuel": 85,"Address":"0 "},{"Id": "JTDKDTB39G1117652", "Name": "CH633A", "Plate" :null,"Lat":49.27719,"Lon":-122.910339,"Fuel":79,"Address":"0 "}]}''')
df = json_normalize(data, 'data', ['success', 'error'])
print(df)
Address Fuel Id Lat Lon Name Plate \
0 0 75 JTDKDTB38E1570707 49.277126 -122.909874 AE2 42K None
1 0 85 JTDKDTB36G1138197 49.276950 -122.910110 DN706K None
2 0 79 JTDKDTB39G1117652 49.277190 -122.910339 CH633A None
success error
0 True False
1 True False
2 True False
或者使用json\u规范化
Ex:
import json
import pandas as pd
data = json.loads('''{"success":true,"error":false,"data":[{"Id":"JTDKDTB38E1570707","Name":"AE2 42K", "Plate" :null, "Lat": 49.2771263,"Lon": -122.909874,"Fuel": 75,"Address": "0 "},{"Id" :"JTDKDTB36G1138197", "Name": "DN706K", "Plate" :null,"Lat" : 49.27695,"Lon": -122.91011,"Fuel": 85,"Address":"0 "},{"Id": "JTDKDTB39G1117652", "Name": "CH633A", "Plate" :null,"Lat":49.27719,"Lon":-122.910339,"Fuel":79,"Address":"0 "}]}''')
df = pd.DataFrame(data)
df["Lat"] = df["data"].apply(lambda x: x["Lat"])
df["Lon"] = df["data"].apply(lambda x: x["Lon"])
df.drop("data", inplace=True, axis=1)
print(df)
error success Lat Lon
0 False True 49.277126 -122.909874
1 False True 49.276950 -122.910110
2 False True 49.277190 -122.910339
import json
from pandas.io.json import json_normalize
data = json.loads('''{"success":true,"error":false,"data":[{"Id":"JTDKDTB38E1570707","Name":"AE2 42K", "Plate" :null, "Lat": 49.2771263,"Lon": -122.909874,"Fuel": 75,"Address": "0 "},{"Id" :"JTDKDTB36G1138197", "Name": "DN706K", "Plate" :null,"Lat" : 49.27695,"Lon": -122.91011,"Fuel": 85,"Address":"0 "},{"Id": "JTDKDTB39G1117652", "Name": "CH633A", "Plate" :null,"Lat":49.27719,"Lon":-122.910339,"Fuel":79,"Address":"0 "}]}''')
df = json_normalize(data, 'data', ['success', 'error'])
print(df)
Address Fuel Id Lat Lon Name Plate \
0 0 75 JTDKDTB38E1570707 49.277126 -122.909874 AE2 42K None
1 0 85 JTDKDTB36G1138197 49.276950 -122.910110 DN706K None
2 0 79 JTDKDTB39G1117652 49.277190 -122.910339 CH633A None
success error
0 True False
1 True False
2 True False
输出:
import json
import pandas as pd
data = json.loads('''{"success":true,"error":false,"data":[{"Id":"JTDKDTB38E1570707","Name":"AE2 42K", "Plate" :null, "Lat": 49.2771263,"Lon": -122.909874,"Fuel": 75,"Address": "0 "},{"Id" :"JTDKDTB36G1138197", "Name": "DN706K", "Plate" :null,"Lat" : 49.27695,"Lon": -122.91011,"Fuel": 85,"Address":"0 "},{"Id": "JTDKDTB39G1117652", "Name": "CH633A", "Plate" :null,"Lat":49.27719,"Lon":-122.910339,"Fuel":79,"Address":"0 "}]}''')
df = pd.DataFrame(data)
df["Lat"] = df["data"].apply(lambda x: x["Lat"])
df["Lon"] = df["data"].apply(lambda x: x["Lon"])
df.drop("data", inplace=True, axis=1)
print(df)
error success Lat Lon
0 False True 49.277126 -122.909874
1 False True 49.276950 -122.910110
2 False True 49.277190 -122.910339
import json
from pandas.io.json import json_normalize
data = json.loads('''{"success":true,"error":false,"data":[{"Id":"JTDKDTB38E1570707","Name":"AE2 42K", "Plate" :null, "Lat": 49.2771263,"Lon": -122.909874,"Fuel": 75,"Address": "0 "},{"Id" :"JTDKDTB36G1138197", "Name": "DN706K", "Plate" :null,"Lat" : 49.27695,"Lon": -122.91011,"Fuel": 85,"Address":"0 "},{"Id": "JTDKDTB39G1117652", "Name": "CH633A", "Plate" :null,"Lat":49.27719,"Lon":-122.910339,"Fuel":79,"Address":"0 "}]}''')
df = json_normalize(data, 'data', ['success', 'error'])
print(df)
Address Fuel Id Lat Lon Name Plate \
0 0 75 JTDKDTB38E1570707 49.277126 -122.909874 AE2 42K None
1 0 85 JTDKDTB36G1138197 49.276950 -122.910110 DN706K None
2 0 79 JTDKDTB39G1117652 49.277190 -122.910339 CH633A None
success error
0 True False
1 True False
2 True False
使用.apply
Ex:
import json
import pandas as pd
data = json.loads('''{"success":true,"error":false,"data":[{"Id":"JTDKDTB38E1570707","Name":"AE2 42K", "Plate" :null, "Lat": 49.2771263,"Lon": -122.909874,"Fuel": 75,"Address": "0 "},{"Id" :"JTDKDTB36G1138197", "Name": "DN706K", "Plate" :null,"Lat" : 49.27695,"Lon": -122.91011,"Fuel": 85,"Address":"0 "},{"Id": "JTDKDTB39G1117652", "Name": "CH633A", "Plate" :null,"Lat":49.27719,"Lon":-122.910339,"Fuel":79,"Address":"0 "}]}''')
df = pd.DataFrame(data)
df["Lat"] = df["data"].apply(lambda x: x["Lat"])
df["Lon"] = df["data"].apply(lambda x: x["Lon"])
df.drop("data", inplace=True, axis=1)
print(df)
error success Lat Lon
0 False True 49.277126 -122.909874
1 False True 49.276950 -122.910110
2 False True 49.277190 -122.910339
import json
from pandas.io.json import json_normalize
data = json.loads('''{"success":true,"error":false,"data":[{"Id":"JTDKDTB38E1570707","Name":"AE2 42K", "Plate" :null, "Lat": 49.2771263,"Lon": -122.909874,"Fuel": 75,"Address": "0 "},{"Id" :"JTDKDTB36G1138197", "Name": "DN706K", "Plate" :null,"Lat" : 49.27695,"Lon": -122.91011,"Fuel": 85,"Address":"0 "},{"Id": "JTDKDTB39G1117652", "Name": "CH633A", "Plate" :null,"Lat":49.27719,"Lon":-122.910339,"Fuel":79,"Address":"0 "}]}''')
df = json_normalize(data, 'data', ['success', 'error'])
print(df)
Address Fuel Id Lat Lon Name Plate \
0 0 75 JTDKDTB38E1570707 49.277126 -122.909874 AE2 42K None
1 0 85 JTDKDTB36G1138197 49.276950 -122.910110 DN706K None
2 0 79 JTDKDTB39G1117652 49.277190 -122.910339 CH633A None
success error
0 True False
1 True False
2 True False
输出:
import json
import pandas as pd
data = json.loads('''{"success":true,"error":false,"data":[{"Id":"JTDKDTB38E1570707","Name":"AE2 42K", "Plate" :null, "Lat": 49.2771263,"Lon": -122.909874,"Fuel": 75,"Address": "0 "},{"Id" :"JTDKDTB36G1138197", "Name": "DN706K", "Plate" :null,"Lat" : 49.27695,"Lon": -122.91011,"Fuel": 85,"Address":"0 "},{"Id": "JTDKDTB39G1117652", "Name": "CH633A", "Plate" :null,"Lat":49.27719,"Lon":-122.910339,"Fuel":79,"Address":"0 "}]}''')
df = pd.DataFrame(data)
df["Lat"] = df["data"].apply(lambda x: x["Lat"])
df["Lon"] = df["data"].apply(lambda x: x["Lon"])
df.drop("data", inplace=True, axis=1)
print(df)
error success Lat Lon
0 False True 49.277126 -122.909874
1 False True 49.276950 -122.910110
2 False True 49.277190 -122.910339
import json
from pandas.io.json import json_normalize
data = json.loads('''{"success":true,"error":false,"data":[{"Id":"JTDKDTB38E1570707","Name":"AE2 42K", "Plate" :null, "Lat": 49.2771263,"Lon": -122.909874,"Fuel": 75,"Address": "0 "},{"Id" :"JTDKDTB36G1138197", "Name": "DN706K", "Plate" :null,"Lat" : 49.27695,"Lon": -122.91011,"Fuel": 85,"Address":"0 "},{"Id": "JTDKDTB39G1117652", "Name": "CH633A", "Plate" :null,"Lat":49.27719,"Lon":-122.910339,"Fuel":79,"Address":"0 "}]}''')
df = json_normalize(data, 'data', ['success', 'error'])
print(df)
Address Fuel Id Lat Lon Name Plate \
0 0 75 JTDKDTB38E1570707 49.277126 -122.909874 AE2 42K None
1 0 85 JTDKDTB36G1138197 49.276950 -122.910110 DN706K None
2 0 79 JTDKDTB39G1117652 49.277190 -122.910339 CH633A None
success error
0 True False
1 True False
2 True False
或者使用json\u规范化
Ex:
import json
import pandas as pd
data = json.loads('''{"success":true,"error":false,"data":[{"Id":"JTDKDTB38E1570707","Name":"AE2 42K", "Plate" :null, "Lat": 49.2771263,"Lon": -122.909874,"Fuel": 75,"Address": "0 "},{"Id" :"JTDKDTB36G1138197", "Name": "DN706K", "Plate" :null,"Lat" : 49.27695,"Lon": -122.91011,"Fuel": 85,"Address":"0 "},{"Id": "JTDKDTB39G1117652", "Name": "CH633A", "Plate" :null,"Lat":49.27719,"Lon":-122.910339,"Fuel":79,"Address":"0 "}]}''')
df = pd.DataFrame(data)
df["Lat"] = df["data"].apply(lambda x: x["Lat"])
df["Lon"] = df["data"].apply(lambda x: x["Lon"])
df.drop("data", inplace=True, axis=1)
print(df)
error success Lat Lon
0 False True 49.277126 -122.909874
1 False True 49.276950 -122.910110
2 False True 49.277190 -122.910339
import json
from pandas.io.json import json_normalize
data = json.loads('''{"success":true,"error":false,"data":[{"Id":"JTDKDTB38E1570707","Name":"AE2 42K", "Plate" :null, "Lat": 49.2771263,"Lon": -122.909874,"Fuel": 75,"Address": "0 "},{"Id" :"JTDKDTB36G1138197", "Name": "DN706K", "Plate" :null,"Lat" : 49.27695,"Lon": -122.91011,"Fuel": 85,"Address":"0 "},{"Id": "JTDKDTB39G1117652", "Name": "CH633A", "Plate" :null,"Lat":49.27719,"Lon":-122.910339,"Fuel":79,"Address":"0 "}]}''')
df = json_normalize(data, 'data', ['success', 'error'])
print(df)
Address Fuel Id Lat Lon Name Plate \
0 0 75 JTDKDTB38E1570707 49.277126 -122.909874 AE2 42K None
1 0 85 JTDKDTB36G1138197 49.276950 -122.910110 DN706K None
2 0 79 JTDKDTB39G1117652 49.277190 -122.910339 CH633A None
success error
0 True False
1 True False
2 True False
输出:
import json
import pandas as pd
data = json.loads('''{"success":true,"error":false,"data":[{"Id":"JTDKDTB38E1570707","Name":"AE2 42K", "Plate" :null, "Lat": 49.2771263,"Lon": -122.909874,"Fuel": 75,"Address": "0 "},{"Id" :"JTDKDTB36G1138197", "Name": "DN706K", "Plate" :null,"Lat" : 49.27695,"Lon": -122.91011,"Fuel": 85,"Address":"0 "},{"Id": "JTDKDTB39G1117652", "Name": "CH633A", "Plate" :null,"Lat":49.27719,"Lon":-122.910339,"Fuel":79,"Address":"0 "}]}''')
df = pd.DataFrame(data)
df["Lat"] = df["data"].apply(lambda x: x["Lat"])
df["Lon"] = df["data"].apply(lambda x: x["Lon"])
df.drop("data", inplace=True, axis=1)
print(df)
error success Lat Lon
0 False True 49.277126 -122.909874
1 False True 49.276950 -122.910110
2 False True 49.277190 -122.910339
import json
from pandas.io.json import json_normalize
data = json.loads('''{"success":true,"error":false,"data":[{"Id":"JTDKDTB38E1570707","Name":"AE2 42K", "Plate" :null, "Lat": 49.2771263,"Lon": -122.909874,"Fuel": 75,"Address": "0 "},{"Id" :"JTDKDTB36G1138197", "Name": "DN706K", "Plate" :null,"Lat" : 49.27695,"Lon": -122.91011,"Fuel": 85,"Address":"0 "},{"Id": "JTDKDTB39G1117652", "Name": "CH633A", "Plate" :null,"Lat":49.27719,"Lon":-122.910339,"Fuel":79,"Address":"0 "}]}''')
df = json_normalize(data, 'data', ['success', 'error'])
print(df)
Address Fuel Id Lat Lon Name Plate \
0 0 75 JTDKDTB38E1570707 49.277126 -122.909874 AE2 42K None
1 0 85 JTDKDTB36G1138197 49.276950 -122.910110 DN706K None
2 0 79 JTDKDTB39G1117652 49.277190 -122.910339 CH633A None
success error
0 True False
1 True False
2 True False