अग्रणी या पिछली सफेद जगह को हटाने के लिए, स्ट्रिप() विधि का उपयोग करें। सबसे पहले, 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()
उदाहरण
पूरा कोड निम्नलिखित है -
import pandas as pd
# create a dataframe with 3 columns
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]})
# removing whitespace from more than 1 column
dataFrame['Product Category'].str.strip()
dataFrame['Product Name'].str.strip()
# dataframe
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