简体   繁体   English

我无法从数据帧保存到 postgresql

[英]I can't save from dataframe to postgresql

import pandas as pd
import requests as rq
from sqlalchemy import create_engine

engine = create_engine('postgresql+psycopg2://postgres:3434@127.0.0.1/postgres')

temp = pd.DataFrame()
df = pd.DataFrame()

vehicleList = {"LX59ANR", "SN63NBK", "YY64GRU"}

for ids in vehicleList:
    r = rq.get('https://api.tfl.gov.uk/Vehicle/' + ids + '/Arrivals')
    r = r.text
    temp = pd.read_json(r)

    temp['Type'] = ids

    df = pd.concat([df, temp], sort=False).reset_index(drop=True)
    df.head(0).to_sql('tfl_bus', engine, if_exists='replace', index=False)  # truncates the table

Hello.你好。 cannot save data from pandas(dataframe) to postgresql.无法将数据从 pandas(dataframe) 保存到 postgresql。 only column names occurred.只出现列名。

在此处输入图片说明

I removed head(0) result like this我像这样删除了 head(0) 结果在此处输入图片说明

df.head(0) needs to be replaced with just df . df.head(0)需要替换为df

The head(0) strips away the actual data leaving the columns... head(0)去除了离开列的实际数据......

This work , I added this line : df['timing'] = list(map(lambda x: json.dumps(x), df['timing']))这项工作,我添加了这一行: df['timing'] = list(map(lambda x: json.dumps(x), df['timing']))

import sqlalchemy as sa
import psycopg2
import requests as rq
import pandas as pd
import json

r = rq.get('https://api.tfl.gov.uk/Vehicle/SN63NBK/Arrivals')
temp = pd.DataFrame()
df = pd.DataFrame()
r = r.text
temp = pd.read_json(r)
temp['Type'] = '1'
df = pd.concat([df, temp], sort=False).reset_index(drop=True) 
engine=sa.create_engine('postgresql+psycopg2://postgres:3434@127.0.0.1/postgres')
df['timing'] = list(map(lambda x: json.dumps(x), df['timing']))
df.to_sql('tfl_bus2', engine, if_exists='replace', index=False)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM