Python 类型错误:字符串索引必须为整数

Python 类型错误:字符串索引必须为整数,python,pandas,Python,Pandas,当我在Dataframe上面运行时,它显示一个错误字符串,索引必须是整数。 我不知道如何解决这个问题。在调用时间之前添加df初始化 `df=城市_数据() 时间(df) ` 在这种情况下,我可以看到如下输出: 06 dtype:int64在调用时间之前添加df初始化 `df=城市_数据() 时间(df) ` 在这种情况下,我可以看到如下输出: 06 dtype:int64代码的问题是df最初不存在。您已经创建了city\u data(),但您没有调用它: 请尝试以下代码: import pand

当我在Dataframe上面运行时,它显示一个错误字符串,索引必须是整数。
我不知道如何解决这个问题。

在调用时间之前添加df初始化

`df=城市_数据()

时间(df) `

在这种情况下,我可以看到如下输出:

06


dtype:int64在调用时间之前添加df初始化

`df=城市_数据()

时间(df) `

在这种情况下,我可以看到如下输出:

06


dtype:int64

代码的问题是
df
最初不存在。您已经创建了
city\u data()
,但您没有调用它:

请尝试以下代码:

import pandas as pd
def city_data():

    city = {'City' : pd.Series(['Ithaca', 'Willingboro', 'Holyoke', 'Abilene', 'New York']),
        'Shape Reported': pd.Series(['Triangle', 'Other', 'Oval', 'Disk', 'Light']), 
        'State': pd.Series(['NY', 'NJ', 'CO', 'KS', 'NY']), 
       'Start Time': pd.Series(['6/1/1930 22:00', '6/30/1930 20:00', '2/15/1931 13:00', '6/1/1931 13:00', '4/18/1933 19:00' ])}
    df = pd.DataFrame(city)
    df['Start Time'] = pd.to_datetime(df['Start Time'])
    return df

def time(df):
    df['month'] = df['Start Time'].dt.month
    common_month = df['month'].mode()
    print(common_month)
time(df)  
输出:

import pandas as pd
def city_data():

    city = {'City' : pd.Series(['Ithaca', 'Willingboro', 'Holyoke', 'Abilene', 'New York']),
    'Shape Reported': pd.Series(['Triangle', 'Other', 'Oval', 'Disk', 'Light']),
    'State': pd.Series(['NY', 'NJ', 'CO', 'KS', 'NY']),
        'Start Time': pd.Series(['6/1/1930 22:00', '6/30/1930 20:00', '2/15/1931 13:00', '6/1/1931 13:00', '4/18/1933 19:00' ])}
     df = pd.DataFrame(city)
     df['Start Time'] = pd.to_datetime(df['Start Time'])
     return df

def time(df):
     df['month'] = df['Start Time'].dt.month
     common_month = df['month'].mode()
     print(common_month)

df = city_data() # Call city_data
print(df)
time(df) # Pass returned data to time

希望这能回答你的问题

代码的问题是,
df
最初不存在。您已经创建了
city\u data()
,但您没有调用它:

请尝试以下代码:

import pandas as pd
def city_data():

    city = {'City' : pd.Series(['Ithaca', 'Willingboro', 'Holyoke', 'Abilene', 'New York']),
        'Shape Reported': pd.Series(['Triangle', 'Other', 'Oval', 'Disk', 'Light']), 
        'State': pd.Series(['NY', 'NJ', 'CO', 'KS', 'NY']), 
       'Start Time': pd.Series(['6/1/1930 22:00', '6/30/1930 20:00', '2/15/1931 13:00', '6/1/1931 13:00', '4/18/1933 19:00' ])}
    df = pd.DataFrame(city)
    df['Start Time'] = pd.to_datetime(df['Start Time'])
    return df

def time(df):
    df['month'] = df['Start Time'].dt.month
    common_month = df['month'].mode()
    print(common_month)
time(df)  
输出:

import pandas as pd
def city_data():

    city = {'City' : pd.Series(['Ithaca', 'Willingboro', 'Holyoke', 'Abilene', 'New York']),
    'Shape Reported': pd.Series(['Triangle', 'Other', 'Oval', 'Disk', 'Light']),
    'State': pd.Series(['NY', 'NJ', 'CO', 'KS', 'NY']),
        'Start Time': pd.Series(['6/1/1930 22:00', '6/30/1930 20:00', '2/15/1931 13:00', '6/1/1931 13:00', '4/18/1933 19:00' ])}
     df = pd.DataFrame(city)
     df['Start Time'] = pd.to_datetime(df['Start Time'])
     return df

def time(df):
     df['month'] = df['Start Time'].dt.month
     common_month = df['month'].mode()
     print(common_month)

df = city_data() # Call city_data
print(df)
time(df) # Pass returned data to time
希望这能回答你的问题