💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] # 准备数据 ![](https://box.kancloud.cn/1f34e043c5eb52e02b1d560dc1f2f15a_1034x338.png) 我们把这个csv这个放到代码下面 # 读取数据 ~~~ import pandas as pd df = pd.read_csv('./tips.csv') print(df) ~~~ 输出 ![](https://box.kancloud.cn/a072388a95b2ca1dc4c266097f141f89_1570x712.png) # 指定读取多少行 head里面参数不写,默认读取5行 ~~~ import pandas as pd df = pd.read_csv('./tips.csv') # 指定读取3行 print(df.head(3)) ~~~ 输出 ![](https://box.kancloud.cn/f3f37c51c58cce59e2c88605367b8341_1026x268.png) # 返回当前信息 ~~~ import pandas as pd df = pd.read_csv('./tips.csv') print(df.info()) ~~~ 输出 ~~~ <class 'pandas.core.frame.DataFrame'> RangeIndex: 244 entries, 0 to 243 Data columns (total 7 columns): total_bill 244 non-null float64 tip 244 non-null float64 sex 244 non-null object smoker 244 non-null object day 244 non-null object time 244 non-null object size 244 non-null int64 dtypes: float64(2), int64(1), object(4) memory usage: 13.4+ KB None ~~~ 结构是pandas.core.frame.DataFrame,DataFrame结构,矩阵 RangeIndex索引有244个,0到243 Data columns数据有7列 下面就是列的名字了 float64表示类型,object是string串 memory usage占用内存多大 # 打印索引值 ~~~ import pandas as pd df = pd.read_csv('./tips.csv') print(df.index) ~~~ 输出 ~~~ RangeIndex(start=0, stop=244, step=1) ~~~ # 打印value值 ~~~ import pandas as pd df = pd.read_csv('./tips.csv') print(df.values) ~~~ 输出 ~~~ [[16.99 1.01 'Female' ..., 'Sun' 'Dinner' 2] [10.34 1.66 'Male' ..., 'Sun' 'Dinner' 3] [21.01 3.5 'Male' ..., 'Sun' 'Dinner' 3] ..., [22.67 2.0 'Male' ..., 'Sat' 'Dinner' 2] [17.82 1.75 'Male' ..., 'Sat' 'Dinner' 2] [18.78 3.0 'Female' ..., 'Thur' 'Dinner' 2]] ~~~ # 打印每个列的dtype值 ~~~ import pandas as pd df = pd.read_csv('./tips.csv') print(df.dtypes) ~~~ 输出 ~~~ total_bill float64 tip float64 sex object smoker object day object time object size int64 dtype: object ~~~