登录爬虫数据库/pandas数据分析操作(去重、选取特定列、选取特定时间段内的数据、字段名称按指定顺序排列)

**1)连接数据库**
一、建立数据库连接
----------------
参考:https://blog.csdn.net/babados/article/details/78108136
接入PostgreSQL数据库: psql -h IP地址 -p 端口  -U **数据库名**
 psql -U username -h ipaddress -d dbname
**之后会要求输入数据库密码**
二、访问数据库
1、列举数据库:\l
2、选择数据库:\c  数据库名
3、查看该某个库中的所有表:\dt
4、切换数据库:\c interface
5、查看某个库中的某个表结构:\d 表名
6、查看某个库中某个表的记录:select * from apps limit 1;
7、显示字符集:\encoding
8、退出psgl:\q
参考:psql -h 192.168.31.157 -p 5432 decision_engine_dev  postgres
-h后面是IP,-P后面是端口,decision_engine_dev为用户名,postgres为数据库名;
错误版:
psql -h sc-db.cfdjbes8ghlt.rds.cn-north-1.amazonaws.com.cn -p 5432 crawler postgres
Password for user postgres: 
postgres为数据库名;
psql -h sc-db.cfdjbes8ghlt.rds.cn-north-1.amazonaws.com.cn -p 5432 crawler
没有,默认当前用户名为数据库名; 
Password for user sc: 
更正:psql -h sc-db.cfdjbes8ghlt.rds.cn-north-1.amazonaws.com.cn -p 5432 crawler 
crawler
后面应当是crawler为数据库名,
综合可以看出,需要指定数据库表名
这样才会得到:
Password for user crawler: 
正确版:
psql -h sc-db.cfdjbes8ghlt.rds.cn-north-1.amazonaws.com.cn -U crawler -d crawler -p 5432
**2)pandas数据列操作**
**1)pandas去重**
df_check_1000=df_check_1000.drop_duplicates()
**2)pandas选取特定的列**
df_ch2 = df_ch1[(df_ch1['add_companyname'].isnull()) & (df_ch1['shixin_cnt'] != 1)] )
**3)\copy的命令,选取特定时间段内的数据:**
\copy (select company_name  from court_shixin_company_new where publish_date between to_date('2018-06-20','yyyy-MM-dd') and to_date('2018-07-13','yyyy-MM-dd'))  to '/home/sc/Downloads/staticdata/t_shixin_check_data.csv'  with csv header;
**4)pandas的Dataframe的列按特定顺序排列**
cols = ['company_name','established_years',
       'industry_dx_rate', 'regcap_change_cnt', 'industry_dx_cnt',
       'address_change_cnt', 'network_share_cancel_cnt', 'cancel_cnt',
       'fr_change_cnt', 'network_share_zhixing_cnt',
       'network_share_judge_doc_cnt', 'judge_doc_cnt', 'share_change_cnt',
       'industry_all_cnt', 'network_share_or_pos_shixin_cnt',
       'judgedoc_cnt']
print("hahahhaha")
print(df_ch2.columns)
df_ch22 = df_ch2.ix[:, cols]

猜你喜欢

转载自blog.csdn.net/sinat_26566137/article/details/81033187