How can I download a json Dataframe in Google Colab? - google-colaboratory

I've been working with a dataframe in google colab, and i convert it to a json format using df.to_json(), now i stuck on how to download it into my local disk (or google drive). i found the answers but in csv format, not json.
Any help is appreciated - thanks!

You can proceed in steps: 1) save the output of to_json() to a file on the Colab backend. Then, use the built-in download helper
from google.colab import files
files.download('colab_file_name')
Here's a complete example that starts with a randomly generated DataFrame:
https://colab.research.google.com/drive/1K95vU0gUJW4iJ4FaHxZ1HSQguOP1flEg

Related

How do I download a 7GB tensorflow-dataset in Google Colab without ending the 12 hour limit?

I'm trying to download the wmt14_translate/fr-en dataset from TensorFlow-datasets in Google Colab under the free tier. Downloading the dataset itself is taking me over 12 hours. Is there any alternative using Google Drive or something since I already have the data stored on my laptop.
PS - The file format of the dataset isn't really clear since it does not even end with a '.'.
1[enter image description here]
Upload the dataset to google drive.
Then, in colab,
from google.colab import drive
drive.mount('/content/drive')
You can use wget to download datasets. Downloading by wget utility is much faster than uploading.
Also, if you'll use kaggle dataset someday you can use kaggle datasets download.

Possibility to save uploaded data in Google Colab for reopening

I started recently solving Kaggle competitions, using 2 computers (laptop and PC). Kaggle gives big amout of data for training ML.
The biggest problem for me is downloading that data, it takes about 30 GB, and bigger issue, unzipping it. I was working on my laptop, but I decided to move to PC. I saved the ipynb file and closed laptop.
After opening this file I saw that all unzipped data went missing and I need to spend 2h for downloading and unzipping it once again.
Is it possible to save all unzipped data with this notebook? Or maybe it's stored somewhere on Google Disk?
You can leverage the storage capacity of GoogleDrive. Colab allows you to have this data stored on your Drive and access it from colab notbook as follows:
from google.colab import drive
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import pandas as pd
drive.mount('/content/gdrive')
img = mpimg.imread(r'/content/gdrive/My Drive/top.bmp') # Reading image files
df = pd.read_csv('/content/gdrive/My Drive/myData.csv') # Loading CSV
When it mounts, it would ask you to visit a particular url to grant permission for accessing drive. Just paste the token returned. Needs to be done only once.
The best thing about colab is you can also run shell cmds from code, all you need to do is to prefix the commands with a ! (bang). Useful when you need to unzip etc.
import os
os.chdir('gdrive/My Drive/data') #change dir
!ls
!unzip -q iris_data.zip
df3 = pd.read_csv('/content/gdrive/My Drive/data/iris_data.csv')
Note: Since you have specified that the data is about 30GB, this may not be useful if you are on the free tier provided by Google (as it gives only 15GB per account) you may have to look elsewhere.
You can also visit this particular question for more solutions on Kaggle integration with Google Colab.

Write out file with google colab

Was there a way to write out files with google colab?
For example, if I use
import requests
r = requests.get(url)
Where will those files be stored? Can they be found?
And similarly, can I get the file I outputted via say tensorflow save function
saver=tf.Saver(....)
...
path = saver.save(sess, "./my_model.ckpt")
Thanks!
In your first example, the data is still in r.content. So you also need to save them first with open('data.dat', 'wb').write(r.content)
Then you can download them with files.download
from google.colab import files
files.download('data.dat')
Downloading your model is the same:
files.download('my_model.ckpt')
I found it is easier to first mount your Google drive to the non-persistent VM and then use os.chdir() to change your current working folder.
After doing this, you can do the exactly same stuff as in local machine.
I have a gist listing several ways to save and transfer files between Colab VM and Google drive, but I think mounting Google drive is the easiest approach.
For more details, please refer to mount_your_google_drive.md in this gist
https://gist.github.com/Joshua1989/dc7e60aa487430ea704a8cb3f2c5d6a6

Export Excel files from Google Colab

I use following codes to save some data frame data, in Google Colab. But it is saved in the "local file system", not in my computer nor Google Drive. How can I get the Excel file from there?
Thanks!
writer = pd.ExcelWriter('hey.xlsx')
df.to_excel(writer)
writer.save()
from google.colab import files
files.download('result.csv')
Use Google Chrome. Firefox shows Network Error.
You'll want to upload the file using something like:
from google.colab import files
uploaded = files.upload()
Then, you can pick out the file data using something like uploaded['your_file_here.xls'].
There's a distinct question that includes recipes for working with Excel files in Drive that might be useful:
https://stackoverflow.com/a/47440841/8841057

DSX reading audio file arriving from Watson IOT to Bluemix Object storage

in January I created a project in DSX that was linked to a Bluemix Object Storage. Audio file arriving from Watson IoT platform were saved in this Object Storage and they were loaded automatically in the DSX files section of the project.
I'm no more able to recreate a new project with the same functionality: I'm no more able to add data service and if I configure Object storage it appears as target and not as source.
I need to read .wav files and process them with numpy in a Python notebook.
Any advise?
Can you add a bit more detail? I don't understand what is the issue:
You cannot create new projects in DSX associated to Object Storage?
You wav files are not automatically showing in the DSx Project?
I am assuming you are trying to read .wav file that you uploaded to object storage.
For reading wave format files you would need library like scipy.
scipy library allows you read wav file from file source.
https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.io.wavfile.read.html
For reading data from object storage, you need to use requests and then you would need to save content to GPFS as a file so that you can use it in scipy's read function.
scipy.io.wavfile.read('samplewavefile1.wav')
Then you can use numpy to do whatever you want to
How to manipulate wav file data in Python?
import numpy as np
import scipy.io.wavfile
rate, data = scipy.io.wavfile.read('samplewavefile1.wav')
sin_data = np.sin(data)
print sin_data
Here is link to complete notebook:-
https://github.com/charles2588/bluemixsparknotebooks/blob/master/Python/ReadBinaryfilesfromObjectStorage.ipynb