【Python】分享14條非常實(shí)用的Pandas函數(shù)方法,建議珍藏??!
今天和大家來分享幾個(gè)十分好用的pandas函數(shù),可能平時(shí)并不是特別的常見,但是卻能夠幫助我們在平時(shí)的工作、學(xué)習(xí)當(dāng)中極大的提高效率,小編也希望讀者朋友們在看完本文之后能夠大有收獲
使用ExcelWriter()可以向同一個(gè)excel的不同sheet中寫入對應(yīng)的表格數(shù)據(jù),首先要?jiǎng)?chuàng)建一個(gè)writer對象,傳入的主要參數(shù)為文件名及其路徑
import pandas as pda = pd.DataFrame({"a": [1, 2, 3, 4, 5],"b": [2, 5, 6, 8, 10],})b = pd.DataFrame({"c": [5, 2, 3, 1, 4],"d": [2, 5, 7, 8, 10],})
# 寫入到同一個(gè)excel當(dāng)中去with pd.ExcelWriter("data.xlsx") as writer:a.to_excel(writer, sheet_name="a_sheet", index = False)b.to_excel(writer, sheet_name="b_sheet", index = False)
pipe()方法可以將一連串的函數(shù)以鏈?zhǔn)降慕Y(jié)構(gòu)嵌套在數(shù)據(jù)集當(dāng)中,例如一個(gè)臟數(shù)據(jù)集當(dāng)中有重復(fù)值、空值和極值等等,我們分別建立了3個(gè)函數(shù)來"drop_duplicates","remove_outliers"和"fill_nans"分別處理上面提到的3個(gè)問題,然后用pipe()方法將這三個(gè)函數(shù)以鏈?zhǔn)降慕Y(jié)構(gòu)串聯(lián)起來,作用在同一個(gè)數(shù)據(jù)集上面,代碼如下圖所示
df_preped = (df.pipe(drop_duplicates).pipe(remove_outliers, ['price', 'carat', 'depth']).pipe(encode_categoricals, ['cut', 'color', 'clarity']))
df = pd.Series([1, 6, 7, [46, 56, 49], 45, [15, 10, 12]]).to_frame("dirty")

df.explode("dirty", ignore_index=True)
data = {'name': ['John', 'Mike', 'Tom', 'Greg', 'Jim'],'income': [8000, 9000, 10000, 10000, 20000],'age': [20, 24, 25, 23, 28]}df = pd.DataFrame(data)
我們挑選出收入在8000到10000范圍之內(nèi)的數(shù)據(jù)
df[df['income'].between(8000, 10000, inclusive='neither')]
df = pd.DataFrame({'a':[1,2,3],'b':[0.55,0.66,1.55],'c':['Jack','Tony','Posi']})df.dtypes

df.convert_dtypes().dtypes
pandas當(dāng)中的select_dtypes()方法功能是返回那些指定數(shù)據(jù)類型的列,當(dāng)中的include顧名思義就是篩選出指定數(shù)據(jù)類型的列,例如下面我們挑選出是bool數(shù)據(jù)類型的數(shù)據(jù)來
a = pd.DataFrame({"a": [1, 2, 3, 4, 5],"b": [True, False, False, True, True],"c": ["John", "Tom", "Mike", "Jim", "Dylan"]})
a.select_dtypes(include='bool')
而exclude就是排除掉指定數(shù)據(jù)類型的數(shù)據(jù),將其他類型的數(shù)據(jù)篩選出來
a.select_dtypes(exclude='bool')
pandas當(dāng)中的mask()方法主要是對按照指定的條件,對數(shù)據(jù)集當(dāng)中的數(shù)據(jù)進(jìn)行替換,例如下面數(shù)據(jù)集當(dāng)中對于大于0的數(shù)據(jù)替換成0
df = pd.DataFrame({"A":[12, 4, 5, 44, 1],"B":[5, 2, 54, 3, 2],"C":[20, 16, 7, 3, 8],"D":[14, 3, 17, 2, 6]})

# 將大于10的數(shù)字替換成0df.mask(df > 10, 0)

nlargest和nsmallest的作用在于可以讓我們看到根據(jù)特定的列排序的最大或者是最小的若干列,例如
data = {'name': ['John', 'Mike', 'Tom', 'Greg', 'Jim'],'income': [2500, 4500, 5000, 3000, 2000],'age': [20, 24, 25, 23, 28]}df = pd.DataFrame(data)
我們按照income這一列將數(shù)據(jù)排序,并且顯示出最大的前3行
df.nlargest(3, 'income')
主要用來返回最大值或者是最小值的位置,也就是索引值
data = {'income': [8000, 9000, 10000, 10000, 20000],'age': [20, 24, 25, 23, 28]}df = pd.DataFrame(data, index = ['John', 'Mike', 'Tom', 'Greg', 'Jim'])

df['income'].idxmax()>>>> Jimdf['income'].idxmin()>>> John
在給出指定范圍的前提下,對于數(shù)據(jù)集當(dāng)中超出該范圍的值進(jìn)行更改,例如我們將范圍限定在-4到6之間,超過6的數(shù)字會被設(shè)置為6,超過-4的數(shù)字會被設(shè)置為-4
data = {'col_0': [9, -3, 0, -1, 5],'col_1': [-2, -7, 6, 8, -5]}df = pd.DataFrame(data)

df.clip(-4, 6)
pandas當(dāng)中at_time()方法和between_time()方法主要是用來處理時(shí)間序列的數(shù)據(jù),根據(jù)給出的時(shí)間點(diǎn)或者時(shí)間范圍來篩選出數(shù)據(jù)
index = pd.date_range("2021-08-01", periods=100, freq="H")data = pd.DataFrame({"col": list(range(100))}, index=index)data.head()

data.at_time("14:00")data.between_time("11:00", "12:00")
data = {'name': ['John', 'Mike', 'Tom', 'Greg', 'Jim'],'income': [8000, 9000, 10000, 10000, 20000],'age': [20, 24, 25, 23, 28]}df = pd.DataFrame(data)

# [index, label]df.at[1, "income"]>>> 9000# [index, index]df.iat[2, 3]>>> 1000
pandas還能夠?qū)?shù)據(jù)集當(dāng)中的數(shù)據(jù)進(jìn)行上色,我們可以通過該方法將某些我們覺得重要的數(shù)據(jù)給標(biāo)注出來,例如我們對數(shù)據(jù)集中每列的最大值和最小值標(biāo)注出來
df = pd.DataFrame(np.random.randn(5, 5), columns=list('ABCDE'))# 我們標(biāo)注出最大、最小的值df.style.highlight_max(color = "yellow")


具體的可以去小編另外寫的一篇文章中去看個(gè)究竟:厲害了,Pandas表格還能五彩斑斕的展示數(shù)據(jù),究竟是怎么做到的呢?
s = pd.Series([2, 4, 6, "abcde", np.nan])s.hasnans>>>> True
往期精彩回顧 本站qq群851320808,加入微信群請掃碼:
