0
我试图运行此代码,但它返回这个错误Python的大熊猫“索引”对象有没有属性“海峡”
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'
我该如何解决呢?我找不到它。
谢谢!
df.index是numpy的阵列,而不是一个熊猫系列(因此不能使用该方法Series.str.split) – cmaher
尝试DF .index.to_series()。str.split('\ s \(') – Shijo
Thank you!,it works! – cluna