Python
 Computer >> コンピューター >  >> プログラミング >> Python

PythonPandas-dtypesにキャストされた値でインデックスを作成します


dtypesにキャストされた値でインデックスを作成するには、 index.astype()を使用します パンダのメソッド。まず、必要なライブラリをインポートします-

import pandas as pd

パンダインデックスの作成-

index = pd.Index([50.4, 10.2, 70.5, 110.5, 90.8, 50.6])

パンダのインデックスを表示する-

print("Pandas Index...\n",index)

データ型をint64に変換-

index.astype('int64')

以下はコードです-

import pandas as pd

# Creating Pandas index
index = pd.Index([50.4, 10.2, 70.5, 110.5, 90.8, 50.6])

# 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)

# convert datatype to int64
print("\nIndex object after converting type...\n",index.astype('int64'))
に変換します

出力

これにより、次の出力が生成されます-

Pandas Index...
Float64Index([50.4, 10.2, 70.5, 110.5, 90.8, 50.6], dtype='float64')

Number of elements in the index...
6

The dtype object...
float64

Index object after converting type...
Int64Index([50, 10, 70, 110, 90, 50], dtype='int64')

  1. PythonPandas-マスクで設定された値の新しいインデックスを返します

    マスクで設定された値の新しいインデックスを返すには、 index.putmask()を使用します パンダのメソッド。まず、必要なライブラリをインポートします- import pandas as pd パンダインデックスの作成- index = pd.Index([5, 65, 10, 17, 75, 40]) パンダのインデックスを表示する- print("Pandas Index...\n",index) 値111-で3未満のインデックス値をマスクして配置します print("\nMask...\n",index.putmask(index &

  2. SeaBorn –PythonPandasでポイントプロットを作成する

    seaborn.pointplot()は、ポイントプロットを作成するために使用されます。以下がCSVファイル形式のデータセットであるとしましょう-Cricketers.csv まず、必要な3つのライブラリをインポートします- import seaborn as sb import pandas as pd import matplotlib.pyplot as plt CSVファイルからPandasDataFrameにデータをロードする- dataFrame = pd.read_csv("C:\\Users\\amit_\\Desktop\\Cricketers.csv"