当前位置:   article > 正文

pandas入门_pandas 读取txt

pandas 读取txt

pandas is a fast, powerful, flexible and easy to use open source data analysis and manipulation tool

一、读取文本文件中的数据

# 导入pandas库
import pandas as pd
  • 1
  • 2

1.1 读取csv文件,以逗号作为分隔符。

file_path = "./ratings.csv"
datas = pd.read_csv(file_path)
  • 1
  • 2
# 查看数据的前几行
datas.head()
  • 1
  • 2
userIdmovieIdratingtimestamp
012965.01147880044
113063.51147868817
213075.01147868828
316655.01147878820
418993.51147868510
# 查看数据的形状,返回行数以及列数
datas.shape
(25000095, 4)
  • 1
  • 2
  • 3
# 查看列表名称,以列表的形式返回
datas.columns
Index(['userId', 'movieId', 'rating', 'timestamp'], dtype='object')
  • 1
  • 2
  • 3
# 查看索引值
datas.index
RangeIndex(start=0, stop=25000095, step=1)
  • 1
  • 2
  • 3
# 查看每列的数据类型
datas.dtypes

userId         int64
movieId        int64
rating       float64
timestamp      int64
dtype: object
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

1.2 读取txt文件,以 \t 作为分隔符

file_path = "./demo.txt"
datas = pd.read_csv(file_path, sep='\t', header=None, names=['Year', 'month', 'day'])
datas
  • 1
  • 2
  • 3
Yearmonthday
0201912
1202023
2202137
3202249

1.3 读取excel文件

file_path = "./books.xlsx"
datas = pd.read_excel(file_path)
datas
  • 1
  • 2
  • 3
0201911
1202022
2202133
3202244

1.4 读取MySQL数据表

# 安装sqlalchemy包
!pip install sqlalchemy
  • 1
  • 2
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting sqlalchemy
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/b4/03/8102d7442498ba2dc915673c6617b1d1729cd3c762f275eb83c6bdc78dd0/SQLAlchemy-1.4.40-cp310-cp310-win_amd64.whl (1.6 MB)
     ---------------------------------------- 1.6/1.6 MB 1.2 MB/s eta 0:00:00
Collecting greenlet!=0.4.17
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ac/3f/3af852c44090814ba41b9a4b5bcfd977f49c9fee83d19b65829e164fc11d/greenlet-1.1.3-cp310-cp310-win_amd64.whl (101 kB)
     -------------------------------------- 101.7/101.7 KB 1.5 MB/s eta 0:00:00
Installing collected packages: greenlet, sqlalchemy
Successfully installed greenlet-1.1.3 sqlalchemy-1.4.40


WARNING: You are using pip version 22.0.4; however, version 22.2.2 is available.
You should consider upgrading via the 'D:\Software\Python310\python.exe -m pip install --upgrade pip' command.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

1.4.1 老版本

import pymysql
conn = pymysql.connect(host='127.0.0.1', user='root', password='wangpeng', database='mydatabase', charset='utf8')
datas = pd.read_sql('select * from books', con=conn)
datas
  • 1
  • 2
  • 3
  • 4
D:\Software\anaconda3\lib\site-packages\pandas\io\sql.py:761: UserWarning: pandas only support SQLAlchemy connectable(engine/connection) ordatabase string URI or sqlite3 DBAPI2 connectionother DBAPI2 objects are not tested, please consider using SQLAlchemy
  warnings.warn(
  • 1
  • 2
idnameprice
01数据结构45.0
12操作系统48.0
23计算机网络56.0
34计算机组成原理54.0
45编译原理65.0

1.4.2 新版本

from sqlalchemy import create_engine
sql_statement = 'select * from books'
engine = create_engine('mysql+pymysql://root:wangpeng@localhost:3306/mydatabase?charset=utf8')
datas = pd.read_sql(sql_statement, engine)
datas
  • 1
  • 2
  • 3
  • 4
  • 5
idnameprice
01数据结构45.0
12操作系统48.0
23计算机网络56.0
34计算机组成原理54.0
45编译原理65.0

二、DataFrame和Series

2.1 Series

Series是一种类似于一维数组的对象,它是由一组数据(可以是不同数据类型)以及一组与之相关的数据标签(即索引)组成。

2.1.1 默认创建Series(默认索引)

s1 = pd.Series(['hello', 1, True, 3.5])
s1  # 左侧为索引,右侧为数据

0    hello
1        1
2     True
3      3.5
dtype: object
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
# 获取索引
s1.index
RangeIndex(start=0, stop=4, step=1)
  • 1
  • 2
  • 3
# 获取数据
s1.values
array(['hello', 1, True, 3.5], dtype=object)
  • 1
  • 2
  • 3

2.1.2 创建指定索引的Series

s2 = pd.Series(['a', False, 5], index=[1, 2, 3])
s2

1        a
2    False
3        5
dtype: object
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2.1.3 使用Python字典创建Series

dict_one = {'name': 'wangpeng', 'age': 18, 'province': 'JiangXi'}
s3 = pd.Series(dict_one)
s3

name        wangpeng
age               18
province     JiangXi
dtype: object
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
# 获取值
s3['name']
'wangpeng'
  • 1
  • 2
  • 3
# 获取多个值
s3[['name', 'age']]

name    wangpeng
age           18
dtype: object
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
type(s3['name'])
str
  • 1
  • 2

2.2 DataFrame

DataFrame是一个表格型的数据结构

  • 每列可以是不同的值类型(数值、字符串、布尔值等)
  • 既有行索引index,也有列索引columns
  • 可以被看做由Series组成的字典

2.2.1 根据多个字典序列创建DataFrame

datas = {
    'State': ['New York', 'Michigan', 'Nevada', 'California', 'Florida'],
    'GDP': [14406, 13321, 10003, 12563, 15364]
}
df = pd.DataFrame(datas)
df
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
StateGDP
0New York14406
1Michigan13321
2Nevada10003
3California12563
4Florida15364
df.dtypes

State    object
GDP       int64
dtype: object
  • 1
  • 2
  • 3
  • 4
  • 5
df.columns
Index(['State', 'GDP'], dtype='object')
  • 1
  • 2
df.index
RangeIndex(start=0, stop=5, step=1)
  • 1
  • 2

2.3 从DataFrame中查询出Series

  • 如果只查询一行、一列,返回的是pd.Series
  • 如果查询多行、多列,返回的是pd.DataFrame

2.3.1 查询列

# 查询单列,也就是一个Series
df['State']
  • 1
  • 2
0      New York
1      Michigan
2        Nevada
3    California
4       Florida
Name: State, dtype: object
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
# 查询多列,也就是一个DataFrame
df[['State', 'GDP']]
  • 1
  • 2
df.loc[0]
  • 1
State    New York
GDP         14406
Name: 0, dtype: object
  • 1
  • 2
  • 3
# 返回索引0到3行的数据,包含第三行
df.loc[0:3]
  • 1
  • 2
StateGDP
0New York14406
1Michigan13321
2Nevada10003
3California12563
# 取所有行,限制列(保留GDP列)
df.loc[:, 'GDP']
  • 1
  • 2
0    14406
1    13321
2    10003
3    12563
4    15364
Name: GDP, dtype: int64
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
# 取所有行,限制列(保留State列)
df.loc[:, 'State']
  • 1
  • 2
0      New York
1      Michigan
2        Nevada
3    California
4       Florida
Name: State, dtype: object
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/74982
推荐阅读
相关标签
  

闽ICP备14008679号