简体   繁体   中英

How to use the Pandas 'sep' command in Google Colab?

So, I used Jupyter Notebook and there using the 'sep' command was pretty simple. But now I'm slowly migrating to Google Colab, and while I can find the file and build the DataFrame with 'pd.read_csv()', I can't seem to separate the columns with the 'sep = ' command!

I mounted the Drive and located the file:

import pandas as pd
from google.colab import drive
drive.mount('/content/gdrive')
with open('/content/gdrive/My Drive/wordpress/cousins.csv','r') as f:
  f.read()

Then I built the Dataframe:

df = pd.read_csv('/content/gdrive/My Drive/wordpress/cousins.csv',sep=";")

The dataframe is built, but it is not separated by columns! Below is a screenshot:

Built DataFrame

Last edit: Turns out the problem was with the data I was trying to use, because it also didn't work on Jupyter. There is no problem with the 'sep' command the way it was being used!

PS: I also tried 'sep='.'' and 'sep = ','' to see if it works, and nothing.

I downloaded the data as a 'csv' table from Football-Reference, paste it on excel, saved as a csv (UTF-8), an example of the file can be found here:

Pastebin Example File

This works for me:

My data:

a,b,c
5,6,7
8,9,10

You don't need sep for comma separated file.

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

import pandas as pd

# suppose I have data in my Google Drive in the file path
# GoogleColaboratory/data/so/a.csv
# The folder GoogleColaboratory is in my Google Drive.
df = pd.read_csv('drive/My Drive/GoogleColaboratory/data/so/a.csv')
df.head()

Instead of

df = pd.read_csv('/content/gdrive/My Drive/wordpress/cousins.csv', sep=";")

Use

df = pd.read_csv('/content/gdrive/My Drive/wordpress/cousins.csv', delimiter=";")

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