简体   繁体   中英

How to use CSV while worrking in google colab?

M done with all my user input coding but got stuck where i need to convert this user input data into a csv file. I've done all this coding in google colab,so i dont know how to convert this file into csv,because m not able to run this code on my laptop's hardrive.

here's the code:

import pandas as pd
n=int(input("how many student's record you want to enter?:"))
Name=[]
Class_Sec=[]
Contact_NO=[]
Mothers_Name=[]
Fathers_Name=[]
Emergency_Contact=[]
Hostel_student=[]
Room_allotted=[]
Annual_Fee=[]
hostelstudent=[]
not_hostel=[]
for i in range(0,n):
  Name.append(input("Name:"))
  Class_Sec.append(input("Class/Section:"))
  Contact_NO.append(int(input("Contact No:")))
  Mothers_Name.append(input("Mother's Name:"))
  Fathers_Name.append(input("Father's Name:"))
  Emergency_Contact.append(int(input("Emergency Contact No:")))
  hostelstudent=input("Hostel Student(y/n):")
  Hostel_student.append(hostelstudent)
  if (hostelstudent=="y"):
    Room_allotted.append(int(input("Room Allotted:")))
  else:
    Room_allotted.append(None)
  Annual_Fee.append(int(input("Annual Fees:")))
d={"Name":Name,"Class/Sec":Class_Sec,"Contact No":Contact_NO,"Mother's Name":Mothers_Name,"Father's Name":Fathers_Name,
   "Emergency Contact":Emergency_Contact,"Hostel Student":Hostel_student,"Room Alloted":Room_allotted,"Annual Fee":Annual_Fee,}
df=pd.DataFrame(d)
df.set_index("Name",inplace=True)
print(df)
c=Hostel_student.count("y")
e=Hostel_student.count("n")
import matplotlib.pyplot as plt
labels=["Hostel Students","Non-Hostel Students"]
no_students=[c,e]
colors=['red','blue']
plt.pie(no_students,labels=labels,colors=colors)
plt.title("Comparing hostel students and Non Hostel Students")
plt.show()

Write this before the command print(df) :

from google.colab import drive
drive.mount('drive')
df.to_csv('data.csv')
!cp data.csv "drive/My Drive/"

This allows you to save your file to your Google Drive And then you can download your output from Google Drive.

Even direct statement df.to_csv("data.csv") will work. it will create the file in your running colab environment which can be downloaded

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM