从存储在 PostgreSQL 数据库中的数据中提取特征

Feature extraction from data stored in PostgreSQL database

我有一些数据存储在 PostgreSQL 数据库中,其中包含成本、开始日期、结束日期、国家等字段。Please take a look at the data here.

现在我要做的是从这些数据中提取一些重要的 features/fields 并将它们存储在单独的 CSV 文件或 pandas 数据框中,这样我就可以使用提取的数据进行分析.

有没有python脚本来完成这个任务?请告诉我。谢谢

首先你应该将你的 postgresql table 数据导入数据框,这可以通过 ,

import psycopg2 as pg
import pandas.io.sql as psql

# get connected to the database
connection = pg.connect("dbname=mydatabase user=postgres")

dataframe = psql.frame_query("SELECT * FROM <tablename>", connection)

这里有解释https://gist.github.com/00krishna/9026574。 之后我们可以 select pandas dataframe 中的特定列。这些可以通过 ,

df1 = dataframe[['projectfinancialtype','regionname']] 
# here you can select n number of feature columns which is available in your dataframe i had only took 2 fields of your json

现在,为了将这些特征列放入 csv,我们可以使用如下代码,

df1.to_csv("pathofoutput.csv", cols=['projectfinancialtype','regionname'])
#it will create csv with your feature columns

希望这些有帮助