1、series
建立series
import pandas as pd
countries = ['中國', '美國', '澳大利亞']
countries_s = pd.series(countries)
print(type(countries_s))
print(countries_s)
numbers = [4, 5, 6]
print(pd.series(numbers))
country_dicts =
country_dict_s = pd.series(country_dicts)
# 給索引命名
country_dict_s.index.name = 'code'
# 給資料命名
country_dict_s.name = 'country'
print(country_dict_s)
print(country_dict_s.values)
print(country_dict_s.index)
處理缺失資料
countries = ['中國', '美國', '澳大利亞', none]
print(pd.series(countries))
numbers = [4, 5, 6, none]
print(pd.series(numbers))
country_dicts =
country_dict_s = pd.series(country_dicts)
print(country_dict_s)
# 通過索引判斷資料是存在
# series也可看作定長、有序的字典
print('ch'
in country_dict_s)
print('nz'
in country_dict_s)
print('iloc:', country_dict_s.iloc[1])
print('loc:', country_dict_s.loc['us'])
print(':', country_dict_s['us'])
print('iloc:\n', country_dict_s.iloc[ [0, 2] ])
print()
print('loc:\n', country_dict_s.loc[['us', 'au']])
向量化操作
import numpy as np
s = pd.series(np.random.randint(0, 1000, 10000))
print(s.head())
print(len(s))
2、dataframe
建立dataframe
import pandas as pd
country1 = pd.series()
reprot_2016_df.head()
# 重新命名列名,注意inplace的使用
inplace=true)
reprot_2016_df.head()
4、boolean mask
print(reprot_2016_df.head())
# 過濾 western europe 地區的國家
# only_western_europe = reprot_2016_df['地區'] == 'western europe'
reprot_2016_df[reprot_2016_df['地區'] == 'western europe']
# 過濾 western europe 地區的國家
# 並且排名在10之外
only_western_europe_10 = (reprot_2016_df['地區'] == 'western europe') & (reprot_2016_df['排名'] > 10)
only_western_europe_10
# 疊加 boolean mask 得到最終結果
reprot_2016_df[only_western_europe_10]
# 熟練以後可以寫在一行中
reprot_2016_df[(reprot_2016_df['地區'] == 'western europe') & (reprot_2016_df['排名'] > 10)]
5、層級索引
密碼:j22j
print(reprot_2015_df.head())
# 設定層級索引
report_2015_df2 = reprot_2015_df.set_index(['region', 'country'])
report_2015_df2.head(20)
# level0 索引
report_2015_df2.loc['western europe']
# 兩層索引
report_2015_df2.loc['western europe', 'switzerland']
# 交換分層順序
report_2015_df2.swaplevel()
# 排序分層
report_2015_df2.sort_index(level=0)
Redis基本資料及操作
set set name zhangsan ok get name zhangsan自增increase incr incr key1 integer 1 incr key1 integer 2自減decrease decr decr key1 integer 1檢視 keys keys 1 nam...
pandas基本操作及拼接
二 pandas拼接操作 原教程鏈結,需要自取。pandas是在numpy的基礎上開發的資料處理工具包,提供了快速 靈活的資料結構。它適用於處理一下型別的資料 pandas的主要資料結構 1.用值列表生成series series pd.series 1 3,5 np.nan,6,8 series ...
基本資料操作
浮點數型別 round x,d 對x四捨五入,d是小數擷取的位數 浮點數運算和比較輔助 浮點數可採用科學計數法表示,使用e或e作為冪的符號,以10為基數 格式 e表示a 10 b 4.3e 3 9.6e5 複數型別 數字型別及操作 餘數,模運算 x y 冪運算 x的y次冪 型別間進行混合運算,生成的...