pandas基本操作

pandas基本操作

來自專欄代碼雜貨鋪

import pandas as pdimport numpy as npfood = pd.read_csv(food_info.csv)# 輸出列名print(food.columns)# 輸出第一項 loc索引先行後列print(food.loc[0])# 輸出第4至第7項print(food.loc[3:6])# 直接索引print(food.loc[[2, 5, 10]])# 按照列名輸出print(food[[Zinc_(mg), Copper_(mg)]])# zin是Zinc_(mg)字元串, 不是列上所指的數據zin, copper = food[[Zinc_(mg), Copper_(mg)]]print(food.shape)print(food["Iron_(mg)"])# 添加新列food["Iron_(g)"] = food["Iron_(mg)"]/1000max = food["Iron_(g)"].max()# 取得索引值index = list(food["Iron_(g)"]).index(max)print(index, food.loc[index, "Iron_(g)"]) # 先行後列# 從小到大排序 ascending默認true從小到大 inplace為false則把序號重置# food.sort_values("Iron_(mg)", inplace=True, ascending=True)print(food["Iron_(mg)"].iloc[:3]) # 這種方式才是取從小到大前三個數據# print(food.loc[0:3, ["Iron_(mg)"]]) # !!!!這種方式是從0號到3號數據,1012個數據food.dropna(axis=0)print(True in food.isnull())

推薦閱讀:

精選 | 2018年3月R新包推薦
今日數據行業日報(2017.4.21)
既然做不到懶得心安理得,就要讓自己忙得其所
單身狗怎樣搭訕遛狗小姐姐
3分鐘掌握一個有數小技能:回頭客分析

TAG:數據分析 | Python | pandas包 |