Python में CSV फ़ाइल में पांडा डेटाफ़्रेम लिखने के लिए, to_csv() . का उपयोग करें तरीका। सबसे पहले, हम सूचियों का एक शब्दकोश बनाते हैं -
# dictionary of lists
d = {'Car': ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'],'Date_of_purchase': ['2020-10-10', '2020-10-12', '2020-10-17', '2020-10-16', '2020-10-19', '2020-10-22']
} अब, सूचियों के उपरोक्त शब्दकोश से पांडा डेटाफ़्रेम बनाएं -
dataFrame = pd.DataFrame(d)
हमारी आउटपुट CSV फ़ाइल डेस्कटॉप पर जनरेट होगी क्योंकि हमने नीचे डेस्कटॉप पथ सेट किया है -
dataFrame.to_csv("C:\\Users\\amit_\\Desktop\\sales1.csv\\SalesRecords.csv") उदाहरण
निम्नलिखित कोड है -
import pandas as pd
# dictionary of lists
d = {'Car': ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'],'Date_of_purchase': ['2020-10-10', '2020-10-12', '2020-10-17', '2020-10-16', '2020-10-19', '2020-10-22']
}
# creating dataframe from the above dictionary of lists
dataFrame = pd.DataFrame(d)
print("DataFrame...\n",dataFrame)
# write dataFrame to SalesRecords CSV file
dataFrame.to_csv("C:\\Users\\amit_\\Desktop\\SalesRecords.csv")
# display the contents of the output csv
print("The output csv file written successfully and generated...") आउटपुट
यह निम्नलिखित आउटपुट उत्पन्न करेगा -
DataFrame... Car Date_of_purchase 0 BMW 2020-10-10 1 Lexus 2020-10-12 2 Audi 2020-10-17 3 Mercedes 2020-10-16 4 Jaguar 2020-10-19 5 Bentley 2020-10-22 The output csv file written successfully and generated...
परिणामी "SalesRecords.csv" निम्नलिखित रिकॉर्ड के साथ सफलतापूर्वक उत्पन्न हुआ, जैसे, पांडा डेटाफ़्रेम -
