0

I'm trying to run this code, but it returns this error

import pandas as pd

df = pd.read_csv('olympics.csv', index_col=0, skiprows=1)

for col in df.columns:
    if col[:2]=='01':
        df.rename(columns={col:'Gold'+col[4:]}, inplace=True)
    if col[:2]=='02':
        df.rename(columns={col:'Silver'+col[4:]}, inplace=True)
    if col[:2]=='03':
        df.rename(columns={col:'Bronze'+col[4:]}, inplace=True)
    if col[:1]=='№':
        df.rename(columns={col:'#'+col[1:]}, inplace=True)

names_ids = df.index.str.split('\s\(') # split the index by '('

AttributeError: 'Index' object has no attribute 'str'

How can I solve it? I can't find it.

Thank you!

2
  • df.index is a numpy array, not a pandas Series (and so cannot use the method Series.str.split) Commented Feb 10, 2017 at 18:55
  • Try df.index.to_series().str.split('\s(') Commented Feb 10, 2017 at 18:55

1 Answer 1

2

It solves with (posted in a comment by @Shijo)

df.index.to_series().str.split('\s\(')
Sign up to request clarification or add additional context in comments.

1 Comment

If upgrade pandas to last version 0.19.2 then df.index.str.split('\s\(') works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.