小虎技术分享站 - pandas
type: channel
link: https://www.xtigerkin.com/tag/pandas/
items: Pandas读取特定列和sqlite3数据库存储

Pandas读取特定列和sqlite3数据库存储
type: item
date: 2024-09-27T15:02:55+08:00
description: 读取特定的columnreport_headers_arr = ['a', 'b'] pd.read_csv(REPORT_PATH, sep='\t', usecols=report_headers_arr)保存dataframe到sqlite是数据库import sqlite3 conn = sqlite3.connect('example.db') df.to_sql('report', conn, if_exists='replace') conn.commit() conn.close()从sqlite数据库表中读取dataframeimport pandas as pd # Connect to the SQLite database conn = sqlite3.connect('example.db') # Read the data from the SQLite database df = pd.read_sql('SELECT * FROM report', conn) # Close the connection conn.close() 通过groupby实现针对某一列数据得分类In [62]: grouped = df.groupby('A') In [63]: for name, group in grouped: ....: print(name) ....: print(group) ....: bar A B C D 1 bar one 0.254161 1.511763 3 bar three 0.215897 -0.990582 5 bar two -0.077118 1.211526 foo A B C D 0 foo one -0.575247 1.346061 2 foo two -1.143704 1.627081 4 foo two 1.193555 -0.441652 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580