How to save dictionary into csv file in google collab

Viewed 28

Im trying to export a dictionary (output) into csv file from google colab but having trouble executing it.

markers ={}
for c in adata.obs.leiden.cat.categories:
    cell_df = df.loc[df.group1 ==c]
    markers[c] = cell_df.index.tolist()[:]
markers

Output:

{'0': ['LINC02295',
  'AL596202.1',
  'TAS2R5',
  'NOG',
  'AL161644.1',
  'AL163932.1',
  'PIGY-DT',
  'LINC01193'],
  '1': ['PLA2G4C',
  'GLIS3',
  'AC079779.1',
  'LINC00501',
  'NPAS2',
  'SIAH3',
  'AC118658.1',
  'Z98745.2',
  'AC007785.3',
  'WARS2-IT1',
  'TMEM132E',
  'GPC2',
  'AL132801.1']}

My attempted code at trying to convert dictionary into csv file and exporting it from google colab:

from google.colab import drive
drive.mount('drive')

import csv
import os

field_names= ['cluster no.', 'gene_marker']
with open('NoFilt_cluster_marker.csv', 'w') as csvfile:
    writer = csv.DictWriter(csvfile, fieldnames=field_names)
    writer.writeheader()
    writer.writerows(markers)

NoFilt_cluster_marker.to_csv('NoFilt_cluster_marker.csv')
!cp NoFilt_cluster_marker.csv "drive/My Drive/"

Any help is greatly appreciated. Thank you!

0 Answers
Related