python - Pandas - Columns not read though Present -
i have following set of data.
url, team1, team2, win_toss, bat_or_bowl, outcome, win_game, date,day_n_night, ground, rain, duckworth_lewis, match_id, type_of_match "espncricinfo-t20/145227.html","western australia","victoria","victoria","bat","western australia won 8 wickets (with 47 balls remaining)","western australia"," jan 12 2005","1"," western australia cricket association ground,perth","0","0","145227","t20" "espncricinfo-t20/212961.html","australian institute of sports","new zealand academy","new zealand academy","bowl","match tied",""," jul 7 2005 ","0"," albury oval, brisbane","0","0","212961","t20" "espncricinfo-t20/216598.html","air india","new south wales","air india","bowl","air india won 7 wickets (with 5 balls remaining)","air india"," aug 19 2005 ","0"," m chinnaswamy stadium, bangalore","0","0","216598","t20" "espncricinfo-t20/216620.html","karnataka state cricket association xi","bradman xi","bradman xi","bowl","karnataka state cricket association xi won 33 runs","karnataka state cricket association xi"," aug 20 2005 ","0"," m chinnaswamy stadium, bangalore","0","0","216620","t20" "espncricinfo-t20/216633.html","chemplast","bradman xi","chemplast","bat","bradman xi won 6 wickets (with 13 balls remaining)","bradman xi"," aug 20 2005 ","0"," m chinnaswamy stadium, bangalore","0","0","216633","t20"
this python console :
**
>>> import pandas pd >>> df = pd.read_csv("sample.txt" , quotechar = '\"') >>> df.shape (9, 14) >>> df.columns index([u'url', u' team1', u' team2', u' win_toss', u' bat_or_bowl', u' outcome', u' win_game', u' date', u' day_n_night', u' ground', u' rain', u' duckworth_lewis', u' match_id', u' type_of_match'], dtype='object') >>> df.url.head() 0 espncricinfo-t20/145227.html 1 espncricinfo-t20/212961.html 2 espncricinfo-t20/216598.html 3 espncricinfo-t20/216620.html 4 espncricinfo-t20/216633.html name: url, dtype: object >>> df.team1.head() traceback (most recent call last): file "<stdin>", line 1, in <module> file "/usr/local/python27/lib/python2.7/site-packages/pandas/core/generic.py", line 2744, in __getattr__ return object.__getattribute__(self, name) attributeerror: 'dataframe' object has no attribute 'team1' >>> df.iloc[1:2] url team1 \ 1 espncricinfo-t20/212961.html australian institute of sports team2 win_toss bat_or_bowl outcome \ 1 new zealand academy new zealand academy bowl match tied win_game date day_n_night ground rain \ 1 nan jul 7 2005 0 albury oval, brisbane 0 duckworth_lewis match_id type_of_match 1 0 212961 t20
we can see column team1 exists unable retrieve df. error columns except first . please me find problem here ! thanks
you have leading space:
u' team1'
in column raises keyerror
do this:
pd.read_csv("sample.txt" , quotechar = '\"', skipinitialspace=true)
so csv read , ignores leading space
see docs
Comments
Post a Comment