Pandas读取特定列和sqlite3数据库存储
读取特定的column
report_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
数据库表中读取dataframe
import 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
感谢扫码支持