केवल एक कॉलम कास्ट करने के लिए, astype() मेथड का उपयोग करें। आइए पहले 2 कॉलम के साथ एक DataFrame बनाएं। उनमें से एक "float64" प्रकार का है और दूसरा "int64":
dataFrame = pd.DataFrame(
{
"Reg_Price": [7000.5057, 1500, 5000, 8000, 9000.75768, 6000],
"Units": [90, 120, 100, 150, 200, 130]
}
)
प्रकार जांचें:
dataFrame.dtypes
मान लीजिए हमें केवल "Units" कॉलम को int64 से int32 में कास्ट करना है। इसके लिए astype() का उपयोग करें:
dataFrame.astype({'Units': 'int32'}).dtypes
उदाहरण
निम्न कोड है:
import pandas as pd
# DataFrame बनाएं
dataFrame = pd.DataFrame(
{
"Reg_Price": [7000.5057, 1500, 5000, 8000, 9000.75768, 6000],
"Units": [90, 120, 100, 150, 200, 130]
}
)
print("DataFrame ...\n", dataFrame)
print("\nDataFrame Types ...\n", dataFrame.dtypes)
print("\nCast only a single column to int32...")
print("\nUpdated DataFrame Types ...\n", dataFrame.astype({'Units': 'int32'}).dtypes)
आउटपुट
यह निम्न आउटपुट उत्पन्न करेगा:
DataFrame ... Reg_Price Units 0 7000.50570 90 1 1500.00000 120 2 5000.00000 100 3 8000.00000 150 4 9000.75768 200 5 6000.00000 130 DataFrame Types ... Reg_Price float64 Units int64 dtype: object Cast only a single column to int32... Updated DataFrame Types ... Reg_Price float64 Units int32 dtype: object