python - Python Pandas 无法识别 /t 分隔符
问题描述
做这个练习来学习 Pandas。我认为根据错误提供的数据库有问题。
想法?
代码:
import numpy as np
import scipy as sp
import pandas as pd
import matplotlib as mpl
url = 'https://raw.githubusercontent.com/justmarkham/DAT8/master/data/chipotle.tsv'
chipo = pd.read_csv(url, delimiter='/t')
print(chipo.head(10))
输出:
ex22_chipotle.py:7: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators (separators > 1 char and different from '\s+' are interpreted as regex); you can avoid this warning by specifying engine='python'.
chipo = pd.read_csv(url, delimiter='/t')
order_id\tquantity\titem_name\tchoice_description\titem_price
0 1\t1\tChips and Fresh Tomato Salsa\tNULL\t$2.39
1 1\t1\tIzze\t[Clementine]\t$3.39
2 1\t1\tNantucket Nectar\t[Apple]\t$3.39
3 1\t1\tChips and Tomatillo-Green Chili Salsa\tN...
4 2\t2\tChicken Bowl\t[Tomatillo-Red Chili Salsa...
5 3\t1\tChicken Bowl\t[Fresh Tomato Salsa (Mild)...
6 3\t1\tSide of Chips\tNULL\t$1.69
7 4\t1\tSteak Burrito\t[Tomatillo Red Chili Sals...
8 4\t1\tSteak Soft Tacos\t[Tomatillo Green Chili...
9 5\t1\tSteak Burrito\t[Fresh Tomato Salsa, [Ric...
解决方案
Use \t
for separator by tab
:
chipo = pd.read_csv(url, delimiter='\t')
print(chipo.head(10))
0 1 1 Chips and Fresh Tomato Salsa
1 1 1 Izze
2 1 1 Nantucket Nectar
3 1 1 Chips and Tomatillo-Green Chili Salsa
4 2 2 Chicken Bowl
5 3 1 Chicken Bowl
6 3 1 Side of Chips
7 4 1 Steak Burrito
8 4 1 Steak Soft Tacos
9 5 1 Steak Burrito
choice_description item_price
0 NaN $2.39
1 [Clementine] $3.39
2 [Apple] $3.39
3 NaN $2.39
4 [Tomatillo-Red Chili Salsa (Hot), [Black Beans... $16.98
5 [Fresh Tomato Salsa (Mild), [Rice, Cheese, Sou... $10.98
6 NaN $1.69
7 [Tomatillo Red Chili Salsa, [Fajita Vegetables... $11.75
8 [Tomatillo Green Chili Salsa, [Pinto Beans, Ch... $9.25
9 [Fresh Tomato Salsa, [Rice, Black Beans, Pinto... $9.25
推荐阅读
- javascript - 获取和等待后React中的“无法读取未定义的属性”
- python-3.x - 如何获取 ImageDraw 对象的数组?
- r - 如何使用 ggplot 制作条形图,其中 x 轴使用多列?
- c++ - 返回的局部变量的地址-可以忽略警告而不会造成损害,但是正确的方法是什么?
- scala - 如何正确使用Either with IO?
- c# - 从 VBO 绘制编辑的顶点值
- java - 遍历矩阵的元素
- c++ - 如何通过将 Infix 转换为 Postfix,然后将其评估为答案来解决结构问题?
- html - 以角度设置输入的文件路径
- assembly - 返回调用函数 MIPS 程序集