Python Pandas无法识别/ t分隔符

时间:2018-07-31 08:01:07

标签: python 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... 

1 个答案:

答案 0 :(得分:1)

使用\ttab用作分隔符:

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