【问题标题】:How to use the Pandas 'sep' command in Google Colab?如何在 Google Colab 中使用 Pandas 的“sep”命令?
【发布时间】:2025-11-26 08:25:01
【问题描述】:

所以,我使用了 Jupyter Notebook,并且使用“sep”命令非常简单。但现在我正在慢慢迁移到 Google Colab,虽然我可以找到文件并使用“pd.read_csv()”构建 DataFrame,但我似乎无法使用“sep =”命令分隔列!

我安装了驱动器并找到了文件:

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()

然后我构建了数据框:

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

数据框已构建,但没有以列分隔!下面是截图:

Built DataFrame

最后一次编辑:原来问题出在我尝试使用的数据上,因为它在 Jupyter 上也不起作用。 'sep' 命令的使用方式没有问题!

PS:我也试过 'sep='.'' 和 'sep = ','' 看看它是否有效,但没有。

我从 Football-Reference 下载数据为“csv”表,将其粘贴到 excel 中,保存为 csv (UTF-8),可以在此处找到文件示例:

Pastebin Example File

【问题讨论】:

  • 您的数据是什么样的?你能更新一下有问题的吗?
  • @astro123 现在就做到了!
  • 你应该尝试粘贴数据 > 图片。

标签: python pandas google-colaboratory


【解决方案1】:

这对我有用:

我的数据:

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

逗号分隔的文件不需要 sep。

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()

【讨论】:

  • 我在 Jupyter 上也试过该文件,但它也失败了,所以问题出在数据上!
  • 好的,那么,如果您在问题中有一些示例数据,而不是粘贴数据图像,会更好。在文本编辑器中打开您的“cousins.csv”,复制一些行并粘贴它们。或者,您可以将数据粘贴到 pastebin.com 并分享链接。
【解决方案2】:

代替

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

使用

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

【讨论】: