简体   繁体   中英

Read CSV File in Python: Syntax error near unexpected token `('

df = pd.read_csv( "https://raw.githubusercontent.com/Apress/data-analysis-and-visualization-using-python/master/Ch07/Salaries.csv")

The error I get is: bash: syntax error near unexpected token `('

When I switch it to [] is get:

df = pd.read_csv["https://raw.githubusercontent.com/Apress/data-analysis-and-visualization-using-python/master/Ch07/Salaries.csv"]

Filesystem           1K-blocks      Used Available Use% Mounted on
df: =: can't find mount point

I'm unsure if I'm missing code or entering it incorrectly.

You're trying to run python in the shell instead of in python.

Within the shell enter py , python or python3 .

From there, do your normal python commands:

import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/Apress/data-analysis-and-visualization-using-python/master/Ch07/Salaries.csv")

Produces

>>> df
         rank discipline  phd  service     sex  salary
0        Prof          B   56       49    Male  186960
1        Prof          A   12        6    Male   93000
2        Prof          A   23       20    Male  110515
3        Prof          A   40       31    Male  131205
4        Prof          B   20       18    Male  104800
..        ...        ...  ...      ...     ...     ...
73       Prof          B   18       10  Female  105450
74  AssocProf          B   19        6  Female  104542
75       Prof          B   17       17  Female  124312
76       Prof          A   28       14  Female  109954
77       Prof          A   23       15  Female  109646

[78 rows x 6 columns]

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