लीडिंग या ट्रेलिंग व्हाइटस्पेस हटाने के लिए, strip() मेथड का उपयोग करें। सबसे पहले, 3 कॉलम "Product Category", "Product Name" और "Quantity" के साथ एक DataFrame बनाएं:
dataFrame = pd.DataFrame({
'Product Category': [' Computer', ' Mobile Phone', 'Electronics ', 'Appliances', ' Furniture', 'Stationery'],
'Product Name': ['Keyboard', 'Charger', ' SmartTV', 'Refrigerators', ' Chairs', 'Diaries'],
'Quantity': [10, 50, 10, 20, 25, 50]
})
एक से अधिक कॉलम से व्हाइटस्पेस हटाएं:
dataFrame['Product Category'].str.strip() dataFrame['Product Name'].str.strip()
उदाहरण
निम्न पूर्ण कोड है:
import pandas as pd
# 3 कॉलम के साथ एक डेटाफ्रेम बनाएं
dataFrame = pd.DataFrame({
'Product Category': [' Computer', ' Mobile Phone', 'Electronics ', 'Appliances', ' Furniture', 'Stationery'],
'Product Name': ['Keyboard', 'Charger', ' SmartTV', 'Refrigerators', ' Chairs', 'Diaries'],
'Quantity': [10, 50, 10, 20, 25, 50]
})
# एक से अधिक कॉलम से व्हाइटस्पेस हटाएं
dataFrame['Product Category'].str.strip()
dataFrame['Product Name'].str.strip()
# डेटाफ्रेम
print("Dataframe after removing whitespaces...\n", dataFrame)
आउटपुट
यह निम्न आउटपुट उत्पन्न करेगा:
Dataframe after removing whitespaces... Product Category Product Name Quantity 0 Computer Keyboard 10 1 Mobile Phone Charger 50 2 Electronics SmartTV 10 3 Appliances Refrigerators 20 4 Furniture Chairs 25 5 Stationery Diaries 50