ValueError: Wrong number of items in pandas dataframe
ValueError: Wrong number of items in pandas dataframe
我在 运行 以下函数时收到错误 - ValueError:通过 6 的项目数量错误,放置意味着 1。
def encoding(df, column_name: str):
if column_name in df.columns:
dr = df[column_name].values
df['Week Day'] = pd.to_datetime(dr).weekday
df['Month'] = pd.DatetimeIndex(df[column_name]).month
df['feature_week_day'] = encoding_1(df,'Week Day',7)
df['feature_month'] = encoding_1(df,'Month', 12)
return df
def encoding_1(df, column_name: str, period: int):
if column_name in df.columns:
df['sine_' + column_name] = np.sin(2 * np.pi * df[column_name] / period)
df['cosine_' + column_name] = np.cos(2 * np.pi * df[column_name] / period)
return df
我调用这些函数使用:
df = pd.DataFrame({'Id':['ABC123', 'ABC124', 'ABC125', 'ABC126'], 'Date':['2008-01-01','2008-01-02','2020-02-01', '20210419']})
result = encoding(df,'Date')
不确定这里出了什么问题。
函数 encoding_1
return 整个数据框,您将其分配给原始数据框上的列。替换:
# ...
df['feature_week_day'] = encoding_1(df,'Week Day',7)
df['feature_month'] = encoding_1(df,'Month', 12)
# ...
与:
# ...
df = encoding_1(df,'Week Day',7)
df = encoding_1(df,'Month', 12)
# ...
我在 运行 以下函数时收到错误 - ValueError:通过 6 的项目数量错误,放置意味着 1。
def encoding(df, column_name: str):
if column_name in df.columns:
dr = df[column_name].values
df['Week Day'] = pd.to_datetime(dr).weekday
df['Month'] = pd.DatetimeIndex(df[column_name]).month
df['feature_week_day'] = encoding_1(df,'Week Day',7)
df['feature_month'] = encoding_1(df,'Month', 12)
return df
def encoding_1(df, column_name: str, period: int):
if column_name in df.columns:
df['sine_' + column_name] = np.sin(2 * np.pi * df[column_name] / period)
df['cosine_' + column_name] = np.cos(2 * np.pi * df[column_name] / period)
return df
我调用这些函数使用:
df = pd.DataFrame({'Id':['ABC123', 'ABC124', 'ABC125', 'ABC126'], 'Date':['2008-01-01','2008-01-02','2020-02-01', '20210419']})
result = encoding(df,'Date')
不确定这里出了什么问题。
函数 encoding_1
return 整个数据框,您将其分配给原始数据框上的列。替换:
# ...
df['feature_week_day'] = encoding_1(df,'Week Day',7)
df['feature_month'] = encoding_1(df,'Month', 12)
# ...
与:
# ...
df = encoding_1(df,'Week Day',7)
df = encoding_1(df,'Month', 12)
# ...