कॉलम को पॉप करने के लिए pop() का उपयोग करें और insert() विधि का उपयोग करके इसे सम्मिलित करें यानी एक कॉलम को स्थानांतरित करना। सबसे पहले, 3 कॉलम के साथ एक डेटाफ्रेम बनाएं:
dataFrame = pd.DataFrame(
{
"Student": ['Jack', 'Robin', 'Ted', 'Marc', 'Scarlett', 'Kat', 'John'],
"Result": ['Pass', 'Fail', 'Pass', 'Fail', 'Pass', 'Pass', 'Pass'],
"Roll Number": [5, 10, 3, 8, 2, 9, 6]
}
)
कॉलम "Roll Number" को पहली स्थिति में ले जाएं, पहले कॉलम को पॉप करके:
shiftPos = dataFrame.pop("Roll Number")
कॉलम को पहली स्थिति पर सम्मिलित करें:
dataFrame.insert(0, "Roll Number", shiftPos)
उदाहरण
निम्नलिखित कोड है:
import pandas as pd
# Create DataFrame
dataFrame = pd.DataFrame(
{
"Student": ['Jack', 'Robin', 'Ted', 'Marc', 'Scarlett', 'Kat', 'John'],
"Result": ['Pass', 'Fail', 'Pass', 'Fail', 'Pass', 'Pass', 'Pass'],
"Roll Number": [5, 10, 3, 8, 2, 9, 6]
}
)
print("DataFrame ...\n", dataFrame)
# move column "Roll Number" to 1st position
shiftPos = dataFrame.pop("Roll Number")
# insert column on the 1st position
dataFrame.insert(0, "Roll Number", shiftPos)
print("\nUpdated DataFrame after moving a column to the first position...\n", dataFrame)
आउटपुट
यह निम्नलिखित आउटपुट देगा:
DataFrame ... Result Roll Number Student 0 Pass 5 Jack 1 Fail 10 Robin 2 Pass 3 Ted 3 Fail 8 Marc 4 Pass 2 Scarlett 5 Pass 9 Kat 6 Pass 6 John Updated DataFrame after moving a column to the first position... Roll Number Result Student 0 5 Pass Jack 1 10 Fail Robin 2 3 Pass Ted 3 8 Fail Marc 4 2 Pass Scarlett 5 9 Pass Kat 6 6 Pass John