简体   繁体   English

Python pandas的“索引”对象没有属性“ str”

[英]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' AttributeError:“索引”对象没有属性“ str”

How can I solve it? 我该如何解决? I can't find it. 我找不到

Thank you! 谢谢!

解决方法(在@Shijo的评论中发布)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM