व्हाइटस्पेस को हटाने के लिए, चाहे वह लीडिंग हो या ट्रेलिंग, 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]})
एकल कॉलम "Product Category" से व्हाइटस्पेस हटाना:
dataFrame['Product Category'].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 a single column
dataFrame['Product Category'].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