在 Pandas 中使用自定義索引引數建立 DataFrame
要使用一些索引建立 DataFrame,我們可以傳遞一個值列表並將其分配給 DataFrame 類中的索引。
步驟
建立二維、尺寸可變、潛在異構的表格資料,df。
將一個索引列表放入 DataFrame 類索引中。
使用自定義索引來列印 DataFrame。
示例
import pandas as pd df = pd.DataFrame( { "x": [5, 2, 1, 9], "y": [4, 1, 5, 10], "z": [4, 1, 5, 0] } ) print "Input DataFrame is:
", df df = pd.DataFrame( { "x": [5, 2, 1, 9], "y": [4, 1, 5, 10], "z": [4, 1, 5, 0] }, index=["John", "Jacob", "Ally", "Simon"] ) print "With Customized Index:
", df
輸出
Input DataFrame is: x y z 0 5 4 4 1 2 1 1 2 1 5 5 3 9 10 0 With Customized Index: x y z John 5 4 4 Jacob 2 1 1 Ally 1 5 5 Simon 9 10 0
廣告