Python-Pandasインデックスの新しいビューを作成します
Pandas Indexの新しいビューを作成するには、index.view()メソッドを使用します。まず、必要なライブラリをインポートします-
import pandas as pd
パンダインデックスの作成-
index = pd.Index([50, 10, 70, 110, 90, 50, 110, 90, 30])
パンダのインデックスを表示する-
print("Pandas Index...\n",index) 新しいビューを作成する-
res = index.view('uint8')
新しいビューの表示-
print("\nThe new view...\n",res) 同じ基本的な値を共有します-
print("\nView for 0th index...\n",res[0])
print("\nView for 1st index...\n",res[1]) 例
以下はコードです-
import pandas as pd
# Creating Pandas index
index = pd.Index([50, 10, 70, 110, 90, 50, 110, 90, 30])
# Display the Pandas index
print("Pandas Index...\n",index)
# Return the number of elements in the Index
print("\nNumber of elements in the index...\n",index.size)
# Return the dtype of the data
print("\nThe dtype object...\n",index.dtype)
# Create a new view
res = index.view('uint8')
# displaying the new view
print("\nThe new view...\n",res)
# shares the same underlying values
print("\nView for 0th index...\n",res[0])
print("\nView for 1st index...\n",res[1]) 出力
これにより、次の出力が生成されます-
Pandas Index... Int64Index([50, 10, 70, 110, 90, 50, 110, 90, 30], dtype='int64') Number of elements in the index... 9 The dtype object... int64 The new view... [ 50 0 0 0 0 0 0 0 10 0 0 0 0 0 0 0 70 0 0 0 0 0 0 0 110 0 0 0 0 0 0 0 90 0 0 0 0 0 0 0 50 0 0 0 0 0 0 0 110 0 0 0 0 0 0 0 90 0 0 0 0 0 0 0 30 0 0 0 0 0 0 0] View for 0th index... 50 View for 1st index... 0
-
PythonPandas-最後から最初のインデックスに新しいインデックス値を挿入します
最後から最初のインデックスに新しいインデックス値を挿入するには、 index.insert()を使用します 方法。最後のインデックス値-1とパラメータとして挿入する値を設定します。 まず、必要なライブラリをインポートします- import pandas as pd パンダインデックスの作成- index = pd.Index(['Car','Bike','Airplane','Ship','Truck']) インデックスを表示- print("Pandas Index...\n",ind
-
Python –Pandasデータフレームに新しい列を作成します
新しい列を作成するには、作成済みの列を使用します。まず、DataFrameを作成してCSVを読みましょう- dataFrame = pd.read_csv("C:\\Users\\amit_\\Desktop\\SalesRecords.csv") 次に、作成済みの列「Reg_Price」から新しい列「New_Reg_Price」を作成し、各値に100を追加して、新しい列を形成します- dataFrame['New_Reg_Price'] = (dataFrame['Reg_Price'] + 100) 例 以下はコードです- impor