Python数据分析之读取文件读取CSV读取Excel读取MySQL读取MongoDB

Python的数据分析,大部分的教程都是想讲numpy,再讲Dataframe,再讲读取文件。但我看书的时候,前面二章看的实在头晕,所以,我们还是通过读取文件来开始我们的Python数据分析吧。

读取CSV

  • 读取csv通过read_csv读取
import pandas as pd
zhuanti = pd.read_csv(open('C:/Users/luopan/Desktop/xiaozhu.csv',encoding='utf-8'))
zhuanti
  • 设置第一列为索引
import pandas as pd
zhuanti1 = pd.read_csv(open('C:/Users/luopan/Desktop/xiaozhu.csv',encoding='utf-8'),index_col=0)
zhuanti1
  • 设置header,这里把header去掉
import pandas as pd
zhuanti2 = pd.read_csv(open('C:/Users/luopan/Desktop/xiaozhu.csv',encoding='utf-8'),index_col=0,header=None)
zhuanti2
  • 跳过前2行
import pandas as pd
zhuanti3 = pd.read_csv(open('C:/Users/luopan/Desktop/xiaozhu.csv',encoding='utf-8'),skiprows=[1,2],index_col=0)
zhuanti3

读取Excel

  • 利用read_excel读取excel文件
import pandas as pd
test = pd.read_excel('C:/Users/luopan/Desktop/test.xlsx',sheetname='Sheet2',header=None)
test

读取MySQL

import pandas as pd
import pymysql
conn = pymysql.connect(host='localhost', user='root', passwd='123456', db='test', port=3306, charset='utf8')
jianshu = pd.read_sql('select * from jianshu1',conn)
jianshu

读取MongoDB

import pandas as pd
import pymongo
client = pymongo.MongoClient('localhost',port = 27017)
test = client['test']
tieba = test['tieba']
data = pd.DataFrame(list(tieba.find()))
data

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

扫码关注云+社区