将快速熊猫数据帧写入postgres

时间:2017-01-26 14:34:10

标签: python postgresql dataframe copy

我想知道从pandas DataFrame到postges DB中的数据写入数据的最快方法。

1)我尝试了from kivy.app import App from kivy.uix.label import Label from kivy.uix.gridlayout import GridLayout from kivy.uix.textinput import TextInput from kivy.lang import Builder from kivy.uix.scrollview import ScrollView from kivy.core.window import Window from kivy.uix.button import Button from kivy.app import runTouchApp class TableView(GridLayout): Col_Names = ["Date","Vendor","Country","MOU","ASR","AOU"] Label_List =[] MyListOfElements = [['1sample1','1sample2','1sample3','1sample4','1sample5','1sample6'], ['2sample1','2sample2','2sample3','2sample4','2sample5','2sample6'], ['3sample1','3sample2','3sample3','3sample4','3sample5','3sample6'], ['4sample1','4sample2','4sample3','4sample4','4sample5','4sample6']] def AddLables(self,columns, rows): self.LabelRow = [] for i in range(columns): for j in range(rows): lbl = Label(size_hint_y =None, size_hint_x = None , width =30) self.layout.add_widget(lbl) self.LabelRow.append(lbl) self.Label_List.append(self.LabelRow) def AddLabel_Text(self,columns, rows): for i in range(columns): for j in range(rows): self.Label_List[i][j].text = self.MyListOfElements[i][j] def __init__(self,**kwargs): self.layout = GridLayout(cols = len(self.Col_Names), padding =5) self.layout.bind(minimum_height=self.layout.setter('height')) for i in range(len(self.Col_Names)): btn = Button(text=self.Col_Names[i], size_hint_y=None, height=30) self.layout.add_widget(btn) self.AddLables(4,6) self.AddLabel_Text(4,6) if __name__ == '__main__': Table = TableView() runTouchApp(Table.layout) ,但出于某种原因需要实体来复制数据,

2)除了我试过以下之外:

pandas.to_sql

但它返回空列表import io f = io.StringIO() pd.DataFrame({'a':[1,2], 'b':[3,4]}).to_csv(f) cursor = conn.cursor() cursor.execute('create table bbbb (a int, b int);COMMIT; ') cursor.copy_from(f, 'bbbb', columns=('a', 'b'), sep=',') cursor.execute("select * from bbbb;") a = cursor.fetchall() print(a) cursor.close()

所以我有两个问题:将数据从python代码(数据帧)复制到postgres DB的最快方法是什么?在我试过的第二种方法中有什么不正确的?

1 个答案:

答案 0 :(得分:6)

你的第二种方法应该非常快。

您的代码存在两个问题:

  1. 将csv写入f后,您将位于文件的末尾。在开始阅读之前,你需要把你的位置放回到开头。
  2. 编写csv时,需要省略标题和索引
  3. 以下是您的最终代码:

    import io
    f = io.StringIO()
    pd.DataFrame({'a':[1,2], 'b':[3,4]}).to_csv(f, index=False, header=False)  # removed header
    f.seek(0)  # move position to beginning of file before reading
    cursor = conn.cursor()
    cursor.execute('create table bbbb (a int, b int);COMMIT; ')
    cursor.copy_from(f, 'bbbb', columns=('a', 'b'), sep=',')
    cursor.execute("select * from bbbb;")
    a = cursor.fetchall()
    print(a)
    cursor.close()