Load local data files to Colaboratory

后端 未结 9 620
野趣味
野趣味 2020-12-02 08:47

I just wondering that is it possible to load local data files(like .xlsx or .csv files that on my google drive) into Colaboratory?

相关标签:
9条回答
  • 2020-12-02 09:02

    First, executing this cell should create an inline "Choose Files" button

    from google.colab import files
    uploaded = files.upload()
    

    After selecting your file(s), uploaded will be a dictionary of keys (the file names) and values (the encoded file objects). To decode the files for a library such as Pandas, try

    import pandas as pd
    import io
    df = pd.read_csv(io.StringIO(uploaded['filename.csv'].decode('utf-8')))
    

    After this your dataframe df should be ready to go

    0 讨论(0)
  • 2020-12-02 09:07

    To get data from your system to colab try this:

    from google.colab import files
    uploaded = files.upload()
    

    Choose the file you want to upload and hit enter and its done. For example, I have uploaded an image and displayed it using the code below:

    import cv2
    import numpy as np
    from matplotlib import pyplot as plt
    
    img = cv2.imread('image.jpg')
    img_cvt = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    
    plt.imshow(img_cvt)
    plt.show()
    
    0 讨论(0)
  • 2020-12-02 09:08

    Yes, all of these scenarios are supported.

    For recipes to access local and Drive files, check out the I/O example notebook.

    For access to xls files, you'll want to upload the file to Google Sheets. Then, you can use the gspread recipes in the same I/O example notebook.

    A recently added way to upload local files is to use the 'Files' tab in the right hand side drawer.

    From there, you can upload a local file using the 'upload' button.

    (You can also download files by right clicking on them in the file tree.)

    0 讨论(0)
  • 2020-12-02 09:11

    Putting this out there as an alternative for people who prefer another way to upload more files - this basically allows you to upload your files through Google Drive.

    Run the below code (found this somewhere previously but I can't find the source again - credits to whoever wrote it!):

    !apt-get install -y -qq software-properties-common python-software-properties module-init-tools
    !add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
    !apt-get update -qq 2>&1 > /dev/null
    !apt-get -y install -qq google-drive-ocamlfuse fuse
    
    from google.colab import auth
    auth.authenticate_user()
    from oauth2client.client import GoogleCredentials
    creds = GoogleCredentials.get_application_default()
    import getpass
    
    !google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
    vcode = getpass.getpass()
    !echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}
    

    Click on the first link that comes up which will prompt you to sign in to Google; after that another will appear which will ask for permission to access to your Google Drive.

    Then, run this which creates a directory named 'drive', and links your Google Drive to it:

    !mkdir -p drive
    !google-drive-ocamlfuse drive
    

    If you do a !ls now, there will be a directory drive, and if you do a !ls drive you can see all the contents of your Google Drive.

    So for example, if I save my file called abc.txt in a folder called ColabNotebooks in my Google Drive, I can now access it via a path drive/ColabNotebooks/abc.txt

    0 讨论(0)
  • 2020-12-02 09:13

    It's a 2 step process.

    Step 1 : First invoke a file selector with in your colab notebook with the following code

    from google.colab import files
    uploaded = files.upload()
    

    this will take you to a file browser window

    step 2 : To load the content of the file into Pandas dataframe, use the following code

    import pandas as pd
    import io
    df = pd.read_csv(io.StringIO(uploaded['iris.csv'].decode('utf-8')))
    print(df)
    
    0 讨论(0)
  • 2020-12-02 09:14

    You can use this URL for uploading your files in Google Colab:

    https://colab.research.google.com/notebooks/io.ipynb#scrollTo=vz-jH8T_Uk2c
    

    go to Local file system>Downloading files to your local file system Then run the code. After that, browser button will be appeared for you to uploading your files from your PC.

    0 讨论(0)
提交回复
热议问题