pandas的兩種資料型別:Series和DataFrame
首先要明確pandas是做什麼的,在確定是如何做的,通過哪些方式去做的?
pandas是做什麼的?
Python Data Analysis Library 或 pandas 是基於NumPy 的一種工具,該工具是為了解決資料分析任務而建立的。Pandas 納入了大量庫和一些標準的資料模型,提供了高效地操作大型資料集所需的工具。pandas提供了大量能使我們快速便捷地處理資料的函式和方法。你很快就會發現,它是使Python成為強大而高效的資料分析環境的重要因素之一。簡單總結就是處理大型資料的工具,我們生活中常用地資料處理軟體是微軟的Excel,當你學會pandas時候,你會發現pandas比Excel好用太多了。
如何做的或者是如何處理資料的?
簡單的來說pandas只有兩種資料型別,Series和DataFrame,Series你可以簡單的理解為Excel中的行或者列,DataFrame可以理解為整個Excel表格,當然這只是形象的理解,實際上他們的功能要比Excel靈活的多。
Series:一維陣列,與Numpy中的一維array類似。二者與Python基本的資料結構List也很相近。Series如今能儲存不同種資料型別,字串、boolean值、數字等都能儲存在Series中。
Time- Series:以時間為索引的Series。
DataFrame:二維的表格型資料結構。很多功能與R中的data.frame類似。可以將DataFrame理解為Series的容器。以下的內容主要以DataFrame為主。
Panel :三維的陣列,可以理解為DataFrame的容器。
詳細介紹兩種型別:
pandas.Series
class pandas.
Series
(data = None,index = None,dtype = None,name = None,copy = False,fastpath = False )
帶軸標籤的一維ndarray(包括時間序列)。
標籤不一定是唯一的,但必須是可清洗的型別。該物件支援基於整數和基於標籤的索引,並提供了許多方法來執行涉及索引的操作。來自ndarray的統計方法已被覆蓋,以自動排除缺失的資料(目前表示為NaN)。
Series(+, - ,/ ,, *
引數: |
data:array-like,dict或scalar value
index:array-like或Index(1d)
D型:numpy.dtype或無
copy:boolean,預設為False
|
---|
屬性
T |
返回轉置 |
返回物件包含盒裝值的系列。 | |
at |
訪問行/列標籤對的單個值。 |
axes |
返回行軸標籤的列表 |
base |
如果共享基礎資料的記憶體,則返回基礎物件 |
(已棄用)as_blocks()的內部屬性,屬性同義詞 | |
data |
返回基礎資料的資料指標 |
dtype |
返回基礎資料的dtype物件 |
返回基礎資料的dtype物件 | |
flags |
|
ftype |
如果資料稀疏則返回 |
如果資料稀疏則返回 | |
如果我有任何nans,我會回來; 實現各種效能加速 | |
iat |
按整數位置訪問行/列對的單個值。 |
iloc |
純粹基於整數位置的索引,用於按位置選擇。 |
index |
系列的索引(軸標籤)。 |
如果物件中的值是monotonic_increasing,則返回布林值 | |
如果物件中的值是monotonic_decreasing,則返回布林值 | |
如果物件中的值是monotonic_increasing,則返回布林值 | |
如果物件中的值是唯一的,則返回布林值 | |
返回基礎資料項的dtype的大小 | |
ix |
主要基於標籤位置的索引器,具有整數位置回退。 |
loc |
通過標籤或布林陣列訪問一組行和列。 |
返回基礎資料中的位元組數 | |
ndim |
根據定義1,返回基礎資料的維數 |
shape |
返回基礎資料形狀的元組 |
size |
返回基礎資料中的元素數量 |
返回系列為ndarray或ndarray-like取決於dtype |
方法
abs() | 返回具有每個元素的絕對數值的Series / DataFrame。 |
add(other[, level, fill_value, axis]) | 新增系列和其他元素(二元運算子新增)。 |
add_prefix(prefix) | 帶有字串字首的字首標籤。 |
add_suffix(suffix) | 帶有字串字尾的字尾標籤。 |
agg(func[, axis]) | 使用指定軸上的一個或多個操作進行聚合。 |
aggregate(func[, axis]) | 使用指定軸上的一個或多個操作進行聚合。 |
align(other[, join, axis, level, copy, …]) | 使用指定的每個軸索引的連線方法將軸上的兩個物件對齊 |
all([axis, bool_only, skipna, level]) | 返回是否所有元素都是True,可能是在軸上。 |
any([axis, bool_only, skipna, level]) | 返回任何元素在請求的軸上是否為True。 |
append(to_append[, ignore_index, …]) | 連線兩個或更多系列。 |
apply(func[, convert_dtype, args]) | 呼叫Series的值的函式。 |
argmax([axis, skipna]) | (已棄用)..已棄用:: 0.21.0 |
argmin([axis, skipna]) | (已棄用)..已棄用:: 0.21.0 |
argsort([axis, kind, order]) | 覆蓋ndarray.argsort。 |
as_blocks([copy]) | (已棄用)將幀轉換為dtype - >建構函式型別的dict,每個型別都具有同類dtype。 |
as_matrix([columns]) | (DEPRECATED)將幀轉換為Numpy陣列表示。 |
asfreq(freq[, method, how, normalize, …]) | 將TimeSeries轉換為指定的頻率。 |
asof(where[, subset]) | 獲取沒有任何NaN的最後一行(或者在沒有NaN的情況下考慮僅使用DataFrame情況下的列子集的最後一行) |
astype(dtype[, copy, errors]) | 將pandas物件轉換為指定的dtype dtype。 |
at_time(time[, asof]) | 在特定時間選擇值(例如, |
autocorr([lag]) | Lag-N自相關 |
between(left, right[, inclusive]) | 返回boolean Series等效於left <= series <= right。 |
between_time(start_time, end_time[, …]) | 選擇一天中特定時間之間的值(例如,上午9:00-9:30)。 |
bfill([axis, inplace, limit, downcast]) | 同義詞 DataFrame.fillna(method='bfill') |
bool() | 返回單個元素PandasObject的bool。 |
cat | 別名 pandas.core.arrays.categorical.CategoricalAccessor |
clip([lower, upper, axis, inplace]) | 在輸入閾值處修剪值。 |
clip_lower(threshold[, axis, inplace]) | 返回值低於閾值截斷的輸入的副本。 |
clip_upper(threshold[, axis, inplace]) | 輸入的返回副本,其值超過給定值(截斷)。 |
combine(other, func[, fill_value]) | 當一個系列或另一個系列中缺少索引時,使用給定函式對兩個系列執行元素二進位制運算,並帶有可選填充值 |
combine_first(other) | 組合系列值,首先選擇呼叫Series的值。 |
compound([axis, skipna, level]) | 返回請求軸的值的複合百分比 |
compress(condition, *args, **kwargs) | 沿給定軸返回選定的陣列切片作為系列 |
consolidate([inplace]) | (已棄用)使用“合併”內部構件計算NDFrame(每個dtype的資料在單個ndarray中組合在一起)。 |
convert_objects([convert_dates, …]) | (DEPRECATED)嘗試推斷物件列的更好dtype。 |
copy([deep]) | 複製此物件的索引和資料。 |
corr(other[, method, min_periods]) | 計算與其他系列的相關性,不包括缺失值 |
count([level]) | 返回系列中非NA / null觀測值的返回數 |
cov(other[, min_periods]) | 計算與Series的協方差,不包括缺失值 |
cummax([axis, skipna]) | 返回DataFrame或Series軸上的累積最大值。 |
cummin([axis, skipna]) | 返回DataFrame或Series軸上的累積最小值。 |
cumprod([axis, skipna]) | 通過DataFrame或Series軸返回累積產品。 |
cumsum([axis, skipna]) | 返回DataFrame或Series軸上的累積和。 |
describe([percentiles, include, exclude]) | 生成描述性統計資料,總結資料集分佈的集中趨勢,分散和形狀,不包括NaN值。 |
diff([periods]) | 第一個離散的元素差異。 |
div(other[, level, fill_value, axis]) | 系列和其他的浮動劃分,元素方式(二元運算元truediv)。 |
divide(other[, level, fill_value, axis]) | 系列和其他的浮動劃分,元素方式(二元運算元truediv)。 |
divmod(other[, level, fill_value, axis]) | 系列和其他的整數除法和模數,元素方式(二元運算子divmod)。 |
dot(other) | 矩陣乘法與DataFrame或內部產品與Series物件。 |
drop([labels, axis, index, columns, level, …]) | 返回已刪除指定索引標籤的系列。 |
drop_duplicates([keep, inplace]) | 返回系列,刪除重複值。 |
dropna([axis, inplace]) | 返回刪除了缺失值的新系列。 |
dt | 別名 pandas.core.indexes.accessors.CombinedDatetimelikeProperties |
duplicated([keep]) | 指示重複的系列值。 |
eq(other[, level, fill_value, axis]) | 等於系列和其他元素(二元運算子eq)。 |
equals(other) | 確定兩個NDFrame物件是否包含相同的元素。 |
ewm([com, span, halflife, alpha, …]) | 提供指數加權函式 |
expanding([min_periods, center, axis]) | 提供擴充套件轉換。 |
factorize([sort, na_sentinel]) | 將物件編碼為列舉型別或分類變數。 |
ffill([axis, inplace, limit, downcast]) | 同義詞 DataFrame.fillna(method='ffill') |
fillna([value, method, axis, inplace, …]) | 使用指定的方法填充NA / NaN值 |
filter([items, like, regex, axis]) | 根據指定索引中的標籤設定資料框的行或列。 |
first(offset) | 用於基於日期偏移量化時間序列資料的初始時段的便捷方法。 |
first_valid_index() | 返回第一個非NA / null值的索引。 |
floordiv(other[, level, fill_value, axis]) | 系列和其他的整數除法,元素方式(二元運算子floordiv)。 |
from_array(arr[, index, name, dtype, copy, …]) | 從陣列構造系列。 |
from_csv(path[, sep, parse_dates, header, …]) | (已棄用)讀取CSV檔案。 |
ge(other[, level, fill_value, axis]) | 大於或等於系列和其他元素(二元運算子ge)。 |
get(key[, default]) | 從給定鍵的物件獲取專案(DataFrame列,Panel切片等)。 |
get_dtype_counts() | 返回此物件中唯一dtypes的計數。 |
get_ftype_counts() | (DEPRECATED)返回此物件中唯一ftypes的計數。 |
get_value(label[, takeable]) | (DEPRECATED)在傳遞的索引標籤處快速檢索單個值 |
get_values() | 與值相同(但處理稀疏轉換); 是一種觀點 |
groupby([by, axis, level, as_index, sort, …]) | 使用mapper(dict或key函式,將給定函式應用於組,將結果作為系列返回)或通過一系列列的組系列。 |
gt(other[, level, fill_value, axis]) | 大於系列和其他元素(二元運算子gt)。 |
head([n]) | 返回前n行。 |
hist([by, ax, grid, xlabelsize, xrot, …]) | 使用matplotlib繪製輸入序列的直方圖 |
idxmax([axis, skipna]) | 返回最大值的行標籤。 |
idxmin([axis, skipna]) | 返回最小值的行標籤。 |
infer_objects() | 嘗試推斷物件列的更好的dtypes。 |
interpolate([method, axis, limit, inplace, …]) | 根據不同的方法插值。 |
isin(values) | 檢查系列中是否包含值。 |
isna() | 檢測缺失值。 |
isnull() | 檢測缺失值。 |
item() | 將基礎資料的第一個元素作為python標量返回 |
items() | 懶惰地迭代(索引,值)元組 |
iteritems() | 懶惰地迭代(索引,值)元組 |
keys() | 索引的別名 |
kurt([axis, skipna, level, numeric_only]) | 使用Fisher對峰度的定義(正常峰度== 0.0),在請求軸上返回無偏峰度。 |
kurtosis([axis, skipna, level, numeric_only]) | 使用Fisher對峰度的定義(正常峰度== 0.0),在請求軸上返回無偏峰度。 |
last(offset) | 用於基於日期偏移量化時間序列資料的最終時段的便捷方法。 |
last_valid_index() | 返回最後一個非NA / null值的索引。 |
le(other[, level, fill_value, axis]) | 小於或等於系列等,逐元素(二元運算樂)。 |
lt(other[, level, fill_value, axis]) | 少於系列和其他元素(二元運算子lt)。 |
mad([axis, skipna, level]) | 返回請求軸的值的平均絕對偏差 |
map(arg[, na_action]) | 使用輸入對應(字典,系列或函式)對映Series的值。 |
mask(cond[, other, inplace, axis, level, …]) | 返回與self相同形狀的物件,其對應的條目來自self,其中cond為False,否則來自other。 |
max([axis, skipna, level, numeric_only]) | 此方法返回物件中的最大值。 |
mean([axis, skipna, level, numeric_only]) | 返回請求軸的值的平均值 |
median([axis, skipna, level, numeric_only]) | 返回請求軸的值的中值 |
memory_usage([index, deep]) | 返回Series的記憶體使用情況。 |
min([axis, skipna, level, numeric_only]) | 此方法返回物件中的最小值。 |
mod(other[, level, fill_value, axis]) | 系列和其他的模數,元素方式(二元運算子mod)。 |
mode() | 返回資料集的模式。 |
mul(other[, level, fill_value, axis]) | 系列和其他的乘法,元素(二元運算子mul)。 |
multiply(other[, level, fill_value, axis]) | 系列和其他的乘法,元素(二元運算子mul)。 |
ne(other[, level, fill_value, axis]) | 不等於系列和其他元素(二元運算子ne)。 |
nlargest([n, keep]) | 返回最大的n個元素。 |
nonzero() | 返回非零元素的整數索引 |
notna() | 檢測現有(非缺失)值。 |
notnull() | 檢測現有(非缺失)值。 |
nsmallest([n, keep]) | 返回最小的n個元素。 |
nunique([dropna]) | 返回物件中唯一元素的數量。 |
pct_change([periods, fill_method, limit, freq]) | 當前元素和先前元素之間的百分比變化。 |
pipe(func, *args, **kwargs) | 應用func(self,* args,** kwargs) |
plot | 別名 pandas.plotting._core.SeriesPlotMethods |
pop(item) | 返回專案並從框架中刪除。 |
pow(other[, level, fill_value, axis]) | 系列和其他元素的指數冪(二元運算子pow)。 |
prod([axis, skipna, level, numeric_only, …]) | 返回請求軸的值的乘積 |
product([axis, skipna, level, numeric_only, …]) | 返回請求軸的值的乘積 |
ptp([axis, skipna, level, numeric_only]) | 返回最大值和之間的差值 |
put(*args, **kwargs) | 將put方法應用於其values屬性(如果有)。 |
quantile([q, interpolation]) | 給定分位數處的返回值,即la numpy.percentile。 |
radd(other[, level, fill_value, axis]) | 新增系列和其他元素(二元運算子radd)。 |
rank([axis, method, numeric_only, …]) | 沿軸計算數值資料等級(1到n)。 |
ravel([order]) | 將展平的底層資料作為ndarray返回 |
rdiv(other[, level, fill_value, axis]) | 系列和其他的浮動劃分,元素方式(二元運算元rtruediv)。 |
reindex([index]) | 使用可選填充邏輯將系列符合到新索引,將NA / NaN放置在先前索引中沒有值的位置。 |
reindex_axis(labels[, axis]) | (已棄用)使用可選的填充邏輯將系列符合到新索引。 |
reindex_like(other[, method, copy, limit, …]) | 將具有匹配索引的物件返回給我自己。 |
rename([index]) | 更改系列索引標籤或名稱 |
rename_axis(mapper[, axis, copy, inplace]) | 更改索引或列的名稱。 |
reorder_levels(order) | 使用輸入順序重新排列索引級別。 |
repeat(repeats, *args, **kwargs) | 重複系列的元素。 |
replace([to_replace, value, inplace, limit, …]) | 替換給定的值to_replace與價值。 |
resample(rule[, how, axis, fill_method, …]) | 頻率轉換和時間序列重取樣的便捷方法。 |
reset_index([level, drop, name, inplace]) | 使用索引重置生成新的DataFrame或Series。 |
rfloordiv(other[, level, fill_value, axis]) | 系列和其他的整數除法,元素方式(二元運算子rfloordiv)。 |
rmod(other[, level, fill_value, axis]) | 系列和其他的模數,元素方式(二元運算子rmod)。 |
rmul(other[, level, fill_value, axis]) | 系列和其他的乘法,元素(二元運算子rmul)。 |
rolling(window[, min_periods, center, …]) | 提供滾動視窗計算。 |
round([decimals]) | 將系列中的每個值舍入到給定的小數位數。 |
rpow(other[, level, fill_value, axis]) | 系列和其他元素的指數冪(二元運算子rpow)。 |
rsub(other[, level, fill_value, axis]) | 減去序列和其他元素(二元運算子rsub)。 |
rtruediv(other[, level, fill_value, axis]) | 系列和其他的浮動劃分,元素方式(二元運算元rtruediv)。 |
sample([n, frac, replace, weights, …]) | 從物件軸返回隨機的專案樣本。 |
searchsorted(value[, side, sorter]) | 查詢應插入元素以維護順序的索引。 |
select(crit[, axis]) | (DEPRECATED)返回與軸標籤匹配條件對應的資料 |
sem([axis, skipna, level, ddof, numeric_only]) | 在請求的軸上返回均值的無偏標準誤差。 |
set_axis(labels[, axis, inplace]) | 將所需索引分配給給定軸。 |
set_value(label, value[, takeable]) | (DEPRECATED)在傳遞的標籤上快速設定單個值。 |
shift([periods, freq, axis]) | 使用可選的時間頻率按期望的週期數移動索引 |
skew([axis, skipna, level, numeric_only]) | 返回請求軸的無偏偏差,由N-1歸一化 |
slice_shift([periods, axis]) | 相當於移位而不復制資料。 |
sort_index([axis, level, ascending, …]) | 按索引標籤排序系列。 |
sort_values([axis, ascending, inplace, …]) | 按值排序。 |
sortlevel([level, ascending, sort_remaining]) | (已棄用)按所選級別對MultiIndex排序系列。 |
squeeze([axis]) | 擠壓長度1維。 |
std([axis, skipna, level, ddof, numeric_only]) | 返回請求軸上的樣本標準偏差。 |
str | 別名 pandas.core.strings.StringMethods |
sub(other[, level, fill_value, axis]) | 減去序列和其他元素(二元運算子子)。 |
subtract(other[, level, fill_value, axis]) | 減去序列和其他元素(二元運算子子)。 |
sum([axis, skipna, level, numeric_only, …]) | 返回請求軸的值的總和 |
swapaxes(axis1, axis2[, copy]) | 交換軸和交換值軸適當 |
swaplevel([i, j, copy]) | 在MultiIndex中交換i和j級別 |
tail([n]) | 返回最後n行。 |
take(indices[, axis, convert, is_copy]) | 沿軸返回給定位置索引中的元素。 |
to_clipboard([excel, sep]) | 將物件複製到系統剪貼簿。 |
to_csv([path, index, sep, na_rep, …]) | 將Series寫入逗號分隔值(csv)檔案 |
to_dense() | 返回NDFrame的密集表示(與稀疏相反) |
to_dict([into]) | 將Series轉換為{label - > value} dict或dict-like物件。 |
to_excel(excel_writer[, sheet_name, na_rep, …]) | 將系列寫入Excel工作表 |
to_frame([name]) | 將Series轉換為DataFrame |
to_hdf(path_or_buf, key, **kwargs) | 使用HDFStore將包含的資料寫入HDF5檔案。 |
to_json([path_or_buf, orient, date_format, …]) | 將物件轉換為JSON字串。 |
to_latex([buf, columns, col_space, header, …]) | 將物件渲染到表格環境表。 |
to_msgpack([path_or_buf, encoding]) | msgpack(序列化)物件輸入檔案路徑 |
to_period([freq, copy]) | 將Series從DatetimeIndex轉換為具有所需頻率的PeriodIndex(如果未傳遞,則從索引推斷) |
to_pickle(path[, compression, protocol]) | Pickle(序列化)物件到檔案。 |
to_sparse([kind, fill_value]) | 將Series轉換為SparseSeries |
to_sql(name, con[, schema, if_exists, …]) | 將儲存在DataFrame中的記錄寫入SQL資料庫。 |
to_string([buf, na_rep, float_format, …]) | 渲染系列的字串表示形式 |
to_timestamp([freq, how, copy]) | 在期間開始時轉換為時間戳的datetimedex |
to_xarray() | 從pandas物件返回一個xarray物件。 |
tolist() | 返回值列表。 |
transform(func, *args, **kwargs) | 呼叫函式生成類似索引的NDFrame並返回帶有轉換值的NDFrame |
transpose(*args, **kwargs) | 返回轉置,根據定義自我 |
truediv(other[, level, fill_value, axis]) | 系列和其他的浮動劃分,元素方式(二元運算元truediv)。 |
truncate([before, after, axis, copy]) | 在某個索引值之前和之後截斷Series或DataFrame。 |
tshift([periods, freq, axis]) | 如果可用,使用索引的頻率來移動時間索引。 |
tz_convert(tz[, axis, level, copy]) | 將tz感知軸轉換為目標時區。 |
tz_localize(tz[, axis, level, copy, ambiguous]) | 將tz-naive TimeSeries本地化為目標時區。 |
unique() | 返回Series物件的唯一值。 |
unstack([level, fill_value]) | Unstack,又名 |
update(other) | 使用傳遞的Series中的非NA值修改系列。 |
valid([inplace]) | (DEPRECATED)返回沒有空值的系列。 |
value_counts([normalize, sort, ascending, …]) | 返回包含唯一值計數的物件。 |
var([axis, skipna, level, ddof, numeric_only]) | 在請求的軸上返回無偏差異。 |
view([dtype]) | 建立系列的新檢視。 |
where(cond[, other, inplace, axis, level, …]) | 返回與self相同形狀的物件,其對應的條目來自self,其中cond為True,否則來自其他。 |
xs(key[, axis, level, drop_level]) | 返回Series / DataFrame的橫截面(行或列)。 |
pandas.DataFrame
class pandas.
DataFrame
(data = None,index = None,columns = None,dtype = None,copy = False )
具有標記軸(行和列)的二維大小可變,可能異構的表格資料結構。算術運算在行標籤和列標籤上對齊。可以被認為是Series物件的類似dict的容器。主要的熊貓資料結構。
引數: |
資料:numpy ndarray(結構化或同類),dict或DataFrame
index:索引或類似陣列
列:索引或類似陣列
dtype:dtype,預設無
copy:boolean,預設為False
|
---|
屬性
T |
轉置索引和列。 |
at |
訪問行/列標籤對的單個值。 |
axes
|