pandas文件
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = index = list(zip(*arrays))
index = pd.multiindex.from_tuples(tuples, names=['first', 'second'])s = pd.series(np.random.randn(8), index=index)
iterables = [['bar', 'baz', 'foo', 'qux'], ['one', 'two']]
pd.multiindex.from_product(iterables, names=['first', 'second'])
df = pd.dataframe([['bar', 'one'], ['bar', 'two'],['foo', 'one'], ['foo', 'two']],columns=['first', 'second'])
pd.multiindex.from_frame(df)
arrays = [np.array(['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux']),np.array(['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'])]
s = pd.series(np.random.randn(8), index=arrays)
df = pd.dataframe(np.random.randn(8, 4), index=arrays)
# 返回多層索引的指定索引
index.get_level_values(0)
index.get_level_values('second')
df.columns.levels
df[['foo','qux']].columns.levels
df[['foo', 'qux']].columns.to_numpy()
df[['foo', 'qux']].columns.get_level_values(0)
new_mi = df[['foo', 'qux']].columns.remove_unused_levels()
new_mi.levels
# 資料對齊和使用reindex
dfmi.loc(axis=0)[:, :, ['c1', 'c3']]
df2.loc(axis=0)[:, :, ['c1', 'c3']] = -10
# 選取指定的行的名 xs方法這個方法也挺常用的
df.xs('one', level='second')
df.xs('one', level='second', axis=1)
df.xs(('one', 'bar'), level=('second', 'first'), axis=1)
df.xs('one', level='second', axis=1, drop_level=false)
df2 = df.mean(level=0)
df2.reindex(df.index, level=0)
df_aligned, df2_aligned = df.align(df2, level=0)
# 轉換多層索引的等級
df[:5].swaplevel(0, 1, axis=0)
# 使用reorder_levels重新排序級別
df[:5].reorder_levels([1, 0], axis=0)
df.rename(columns=)
df.rename(index=)
df.rename_axis(index=['abc', 'def'])
s.sort_index()
s.sort_index(level=0)
s.sort_index(level=1)
s.index.set_names(['l1', 'l2'], inplace=true)
df.t.sort_index(level=1, axis=1)
dfm = pd.dataframe()
dfm = dfm.set_index(['jim', 'joe'])
dfm.index.is_lexsorted()
dfm.index.lexsort_depth
dfm = dfm.sort_index()
dfm.index.is_lexsorted()
dfm.index.lexsort_depth
fm.loc[(0, 'y'):(1, 'z')]
index = pd.index(np.random.randint(0, 1000, 10))
positions = [0, 9, 3]
index.take(positions)
ser.iloc[positions]
ser.take(positions)
frm = pd.dataframe(np.random.randn(5, 3))
frm.take([1, 4, 3])
frm.take([0, 2], axis=1)
df['b'] = df['b'].astype(categoricaldtype(list('cab')))
df2 = df.set_index('b')
pandas資料的索引操作
coding utf 8 series索引 行索引 import pandas as pd import numpy as np ser obj pd.series range 5 index a b c d e print ser obj 行索引獲單個值 print ser obj b ser o...
pandas索引物件
python for data analysis index物件是不可修改的 immutable 這樣才能使index物件在多個資料結構之間安全共享。in 1 import pandas as pd in 2 from pandas import series,dataframe in 3 impo...
pandas 索引切片
ser1 pd.series range 10,15 index list abcde print ser1 普通索引 print ser1 a print ser1 0 print 注意通過自定義索引的左閉右閉的,用預設索引 下標 是左閉右開的 print ser1 a c print ser1 ...