cluna
cluna

Reputation: 85

Python pandas 'Index' object has no attribute 'str'

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!

Upvotes: 0

Views: 8027

Answers (1)

cluna
cluna

Reputation: 85

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

df.index.to_series().str.split('\s\(')

Upvotes: 2

Related Questions