एक नया कॉलम बनाने के लिए, हम पहले से बने कॉलम का उपयोग करेंगे। पहले, आइए एक डेटाफ्रेम बनाएं और हमारी CSV पढ़ें:
dataFrame = pd.read_csv("C:\\Users\\amit_\\Desktop\\SalesRecords.csv")
अब, हम पहले से बने कॉलम "Reg_Price" से एक नया कॉलम "New_Reg_Price" बनाएंगे और प्रत्येक मान में 100 जोड़कर एक नया कॉलम बनाएंगे:
dataFrame['New_Reg_Price'] = (dataFrame['Reg_Price'] + 100)
उदाहरण
निम्नलिखित कोड है:
import pandas as pd
# CSV फ़ाइल पढ़ें
dataFrame = pd.read_csv("C:\\Users\\amit_\\Desktop\\SalesRecords.csv")
print("DataFrame...\n", dataFrame)
# डेटाफ्रेम में पंक्तियों और कॉलम की गिनती करें
print("\nNumber of rows and column in our DataFrame = ", dataFrame.shape)
dataFrame['New_Reg_Price'] = (dataFrame['Reg_Price'] + 100)
print("Updated DataFrame with a new column...\n", dataFrame)
print("\n[Updated] Number of rows and column in our DataFrame = ", dataFrame.shape)
आउटपुट
यह निम्नलिखित आउटपुट उत्पन्न करेगा:
DataFrame... Car Date_of_Purchase Reg_Price 0 BMW 10/10/2020 1000 1 Lexus 10/12/2020 750 2 Audi 10/17/2020 750 3 Jaguar 10/16/2020 1500 4 Mustang 10/19/2020 1100 5 Lamborghini 10/22/2020 1000 Number of rows and column in our DataFrame = (6, 3) Updated DataFrame with a new column ... Car Date_of_Purchase Reg_Price New_Reg_Price 0 BMW 10/10/2020 1000 1100 1 Lexus 10/12/2020 750 850 2 Audi 10/17/2020 750 850 3 Jaguar 10/16/2020 1500 1600 4 Mustang 10/19/2020 1100 1200 5 Lamborghini 10/22/2020 1000 1100 (Updated)Number of rows and column in our DataFrame = (6, 4)