pandas里对索引的操作主要有
1. DataFrame.rename
2. DataFrame.rename_axis
3. DataFrame.reindex
4. DataFrame.reindex_axis
5. DataFrame.reset_index
6. pandas.Index.reindex
7. pandas.Index.set_names
其中1和2,是对索引的更改,原索引是不变的.3和4是增加和减少了索引,如果索引存在则还按照原来的值,如果不存在则填充空值.5是重新给索引.
1~5都返回的是数据框
6~7返回的是索引
DataFrame.rename(index=None, columns=None, **kwargs)
1.renamed : DataFrame (new object)
In [1]: import pandas as pd
...: df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
...: df
Out[1]:
A B
0 1 4
1 2 5
2 3 6
In [2]: df.rename(index={0:3,1:4,2:5}, columns={"A": "a", "C": "c"})
Out[2]:
a B
3 1 4
4 2 5
5 3 6
DataFrame.rename_axis(mapper, axis=0, copy=True, inplace=False)
In [1]: import pandas as pd
...: df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
...: df
Out[1]:
A B
0 1 4
1 2 5
2 3 6
In [2]: df.rename_axis({0:3,1:4,2:5})
Out[2]:
A B
3 1 4
4 2 5
5 3 6
In [3]: df.rename_axis({"A": "a", "C": "c"},axis=1)
Out[3]:
a B
0 1 4
1 2 5
2 3 6
DataFrame.reindex(index=None, columns=None, **kwargs)
import pandas as pd
df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
df
Out[1]:
A B
0 1 4
1 2 5
2 3 6
df.reindex(index=(1,2,3))
Out[2]:
A B
1 2.0 5.0
2 3.0 6.0
3 NaN NaN
df.reindex(columns=("B","C"))
Out[3]:
B C
0 4 NaN
1 5 NaN
2 6 NaN
DataFrame.reindex_axis(labels, axis=0, method=None, level=None, copy=True, limit=None, fill_value=nan)
In [1]: import pandas as pd
...: df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
...: df
Out[1]:
A B
0 1 4
1 2 5
2 3 6
In [2]: df.reindex_axis((1,2,3))
Out[2]:
A B
1 2.0 5.0
2 3.0 6.0
3 NaN NaN
In [3]: df.reindex_axis(("B","C"),axis=1)
Out[3]:
B C
0 4 NaN
1 5 NaN
2 6 NaN
DataFrame.reset_index(level=None, drop=False, inplace=False, col_level=0, col_fill='')
In [1]: import pandas as pd
...: df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
...: df=df.reindex_axis((1,2,3))
...: df
Out[1]:
A B
1 2.0 5.0
2 3.0 6.0
3 NaN NaN
In [2]: df.reset_index()
Out[2]:
index A B
0 1 2.0 5.0
1 2 3.0 6.0
2 3 NaN NaN
set_index方法是将某一列做为索引,而reset_index是从新按int升序的方式做了一个索引
Index.reindex(target, method=None, level=None, limit=None, tolerance=None)
In [1]: import pandas as pd
...: df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
...: df
Out[1]:
A B
0 1 4
1 2 5
2 3 6
In [2]: df.index.reindex((1,2,3))
Out[2]: (Int64Index([1, 2, 3], dtype='int64'), array([ 1, 2, -1], dtype=int64))
Index.set_names(names, level=None, inplace=False)