Pandas常用方法彙總

2021-10-07 19:06:54 字數 1476 閱讀 4208

如果說numpy是列表的話,pandas就是元素為列表的字典,下面介紹pandas常用方法:

import pandas as pd

import numpy as np

s = pd.series([1,3,6,np.nan,9]) #由一組資料和資料索引組成的資料

print(s)

dates = pd.date_range('20200704',periods=6)

print(dates)

df = pd.dataframe(np.random.randn(6,4),index=dates,columns=['a','b','c','d'])

print(df)

df1 = pd.dataframe(np.arange(12).reshape((3,4)))

print(df1)

df2 = pd.dataframe()

print(df2)

print(df2.dtypes) #計算每列資料型別

print(df2.index) #取行索引

print(df2.columns) #取列標籤

print(df2.values) #取值

print(df2.describe()) #求最大,最小,方差,平均數等

print(df2.t) #轉置

print(df2.sort_index(axis=1,ascending=false)) #列,降序

print(df2.sort_index(axis=0,ascending=true)) #行,公升序

print(df2.sort_values(by='e')) #對某列排序

print(df.loc['20200704']) #通過標籤: loc選擇資料

print(df.loc['20200704',['a','b']])

print(df.iloc[3:5,1:3]) #通過位置: iloc選擇資料

print(df.iloc[[1,3],1:3]) #單獨選擇某行資料

print(df[df.a>1]) #通過boolean indexing選擇資料

df['e'] = pd.series([1,2,3,4,5,6],index=dates) #增加e列資料

print(df)

df.iloc[1,2] = np.nan #修改資料為nan

df.iloc[2,3] = np.nan

print(df.dropna(axis=0,how='any')) #若某行資料有nan,刪除該行所有資料,how='all',若某行所有資料為nan,刪除

print(df.fillna(value=0)) #填上nan的資料為0

print(np.any(df.isnull()) == true) #若包含nan的資料返回true

pandas彙總 1 pandas常用函式

函式說明 pd.isnull series pd.notnull series 判斷是否為空 nan 判斷是否不為空 not nan coding utf 8 author 蔚藍的天空tom aim pandas的常用函式的例程 import pandas as pd defpandas manua...

pandas常用方法

import pandas as pd import numpy as np import matplotlib.pyplot as plt import datetime import redf pd.read csv path file.csv 引數 header none 用預設列名,0,1,...

pandas 常用方法

import pandas as pd pd.read csv filename,encoding utf 8 讀取csv pd.to csv filename 儲存檔案,filename為檔案路徑,可以是相對路徑or絕對路徑 pd.to csv filename,index 0 儲存到檔案時,不要...