多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] # 构造一个DataFrame结构 ~~~ import pandas as pd data = {'country': ['aaa', 'bbb', 'ccc'], 'population': [10, 12, 14]} frame = pd.DataFrame(data) print(frame) ~~~ 输出 ~~~ country population 0 aaa 10 1 bbb 12 2 ccc 14 ~~~ 构造出来的DataFrame也能打印出info这些信息 # 取指定的数据 cvs是这样 ![](https://box.kancloud.cn/dc9f5e502c1c485d15a2d3fdb38dd667_1612x394.png) 我们取出里面的Age值 ~~~ import pandas as pd csv = pd.read_csv('./titanic.csv') print(csv['Age']) ~~~ 输出 左侧0,1,2,3是索引 ![](https://box.kancloud.cn/a7579236bad3f72cdd71debbc0bd0e10_450x458.png) 这些行有空值 如果要取出前5个 `print(csv['Age'][:5])` # 指定索引 ~~~ import pandas as pd csv = pd.read_csv('./titanic.csv') # 设置左侧索引 index = csv.set_index('Name') print(index['Age'][:5]) ~~~ 输出 ~~~ Name Braund, Mr. Owen Harris 22.0 Cumings, Mrs. John Bradley (Florence Briggs Thayer) 38.0 Heikkinen, Miss. Laina 26.0 Futrelle, Mrs. Jacques Heath (Lily May Peel) 35.0 Allen, Mr. William Henry 35.0 Name: Age, dtype: float64 ~~~ # 根据索引找对应的值 ~~~ import pandas as pd csv = pd.read_csv('./titanic.csv') # 设置左侧索引 index = csv.set_index('Name') # 根据名字这个索引找他的年龄 # 索引和年龄 age = index['Age'] print(age['Braund, Mr. Owen Harris']) ~~~ 输出 ~~~ 22.0 ~~~ # 运算 ~~~ import pandas as pd csv = pd.read_csv('./titanic.csv') # 设置左侧索引 index = csv.set_index('Name') # 索引和年龄 age = index['Age'] age1 = age * 10 print(age1) ~~~ 输出 ~~~ Name Braund, Mr. Owen Harris 22.0 Cumings, Mrs. John Bradley (Florence Briggs Thayer) 38.0 Heikkinen, Miss. Laina 26.0 Futrelle, Mrs. Jacques Heath (Lily May Peel) 35.0 Allen, Mr. William Henry 35.0 Moran, Mr. James NaN ~~~ 他的运算不影响原来的值,运算的结果需要新的值类来接收 # 求最小值,最大值,mean值 ~~~ import pandas as pd csv = pd.read_csv('./titanic.csv') # 设置左侧索引 index = csv.set_index('Name') # 索引和年龄 age = index['Age'] # 求最小值 age_min = age.min() print(age_min) # 求最大值 age_max = age.max() print(age_max) # 求mean值 mean = age.mean() print(mean) ~~~ 输出 ~~~ 0.42 80.0 29.6991176471 ~~~ # 观察数据的基本统计特性 ~~~ import pandas as pd csv = pd.read_csv('./titanic.csv') describe = csv.describe() print(describe) ~~~ 输出 ![](https://box.kancloud.cn/909b3224ea925c18a2ffd2f9217f84b4_1228x406.png)