- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在引用Rounak Banik的《Hands onRecommendation Systems with Python》一书尝试Python中基于知识的推荐系统。我们有 IMDB 的电影数据集。
我在最终输出中遇到错误。请参阅下面我的整个代码,在构建图表函数中我收到了错误。请帮我解决这个问题。谢谢。
我无法在此平台上找到类似问题的答案。因此我发布了新问题。
import pandas as pd
import numpy as np
df = pd.read_csv('..../RecoSys/data/movies_metadata.csv')
#print all the features(or columns) of the dataFrame
df.columns
#only keep those that we require
df = df[['title', 'genres', 'release_date', 'runtime', 'vote_average', 'vote_count']]
df.head()
#convert release_date into pandas datetime format
df['release_date'] = pd.to_datetime(df['release_date'], errors='coerce')
df['year'] = df['release_date'].apply(lambda x: str(x).split('-')[0] if x!=np.nan else np.nan)
#Helper function to convert NaT to 0 and all other years to integers.
def convert_int(x):
try:
return int(x)
except:
return 0
#Apply convert_int to the year feature
df['year'] = df['year'].apply(convert_int)
#Drop the release_date column
df = df.drop('release_date', axis=1)
#Display the dataframe
df.head()
#Print genres of the first movie
df.iloc[0]['genres']
#Import the literal_eval function from ast
from ast import literal_eval
import json
#Define a stringified list and output its type
a = "[1,2,3]"
print(type(a))
#Apply literal_eval and output type
b = literal_eval(a)
print(type(b))
#Convert all NaN into stringified empty lists
df['genres'] = df['genres'].fillna('[]')
#Apply literal_eval to convert to the list object
df['genres'] = df['genres'].apply(literal_eval)
#df['genres'] = json.loads(df['genres'])
#Convert list of dictionaries to a list of strings
df['genres'] = df['genres'].apply(lambda x: [i['name'] for i in x] if isinstance(x, list) else [])
df.head()
#Create a new feature by exploding genres
s = df.apply(lambda x: pd.Series(x['genres']),axis=1).stack().reset_index(level=1, drop=True)
#Name the new feature as 'genre'
s.name = 'genre'
#Create a new dataframe gen_df which by dropping the old 'genres' feature and adding the new 'genre'.
gen_df = df.drop('genres', axis=1).join(s)
#Print the head of the new gen_df
gen_df.head(15)
def build_chart(gen_df, percentile=0.8):
#Ask for preferred genres
print("Please Input preferred genre")
genre = input()
#Ask for lower limit of duration
print("Please Input shortest duration")
low_time = int(input())
#Ask for upper limit of duration
print("Please Input Longesr Duration")
high_time = int(input())
#Ask for lower limit of timeline
print("Input earliest year")
low_year = int(input())
#Ask for upper limit of timeline
print("Input latest year")
high_year = int(input())
#Define a new movies variable to store the preferred movies. Copy the contents of gen_df to movies
movies = gen_df.copy()
#Filter based on the condition
movies = movies[(movies['genre'] == genre) &
(movies['runtime'] >= low_time) &
(movies['runtime'] <= high_time) &
(movies['year'] >= low_year) &
(movies['year'] <= high_year)]
#Compute the values of C and m for the filtered movies
C = movies['vote_average'].mean()
m = movies['vote_count'].quantile(percentile)
#Only consider movies that have higher than m votes. Save this in a new dataframe q_movies
q_movies = movies.copy().loc[movies['vote_count'] >= m]
#Calculate score using the IMDB formula
q_movies['score'] = q_movies.apply(lambda x: (x['vote_count']/(x['vote_count']+m) * x['vote_average'])
+ (m/(m+x['vote_count']) * C), axis=1)
#Sort movies in descending order of their scores
q_movies = q_movies.sort_values('score', ascending=False)
return q_movies
build_chart(gen_df).head()
Please Input preferred genre
animation
Please Input shortest duration
30
Please Input Longesr Duration
120
Input earliest year
1990
Input latest year
2005
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\pandas\core\frame.py in _ensure_valid_index(self, value)
3423 try:
-> 3424 value = Series(value)
3425 except (ValueError, NotImplementedError, TypeError):
~\Anaconda3\lib\site-packages\pandas\core\series.py in __init__(self, data, index, dtype, name, copy, fastpath)
263
--> 264 data = SingleBlockManager(data, index, fastpath=True)
265
~\Anaconda3\lib\site-packages\pandas\core\internals\managers.py in __init__(self, block, axis, do_integrity_check, fastpath)
1480 if not isinstance(block, Block):
-> 1481 block = make_block(block, placement=slice(0, len(axis)), ndim=1)
1482
~\Anaconda3\lib\site-packages\pandas\core\internals\blocks.py in make_block(values, placement, klass, ndim, dtype, fastpath)
3094
-> 3095 return klass(values, ndim=ndim, placement=placement)
3096
~\Anaconda3\lib\site-packages\pandas\core\internals\blocks.py in __init__(self, values, placement, ndim)
2630 super(ObjectBlock, self).__init__(values, ndim=ndim,
-> 2631 placement=placement)
2632
~\Anaconda3\lib\site-packages\pandas\core\internals\blocks.py in __init__(self, values, placement, ndim)
86 'Wrong number of items passed {val}, placement implies '
---> 87 '{mgr}'.format(val=len(self.values), mgr=len(self.mgr_locs)))
88
ValueError: Wrong number of items passed 6, placement implies 0
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-53-3c3d1bc1cf24> in <module>
45 return q_movies
46
---> 47 build_chart(gen_df).head()
<ipython-input-53-3c3d1bc1cf24> in build_chart(gen_df, percentile)
39 #Calculate score using the IMDB formula
40 q_movies['score'] = q_movies.apply(lambda x: (x['vote_count']/(x['vote_count']+m) * x['vote_average'])
---> 41 + (m/(m+x['vote_count']) * C), axis=1)
42 #Sort movies in descending order of their scores
43 q_movies = q_movies.sort_values('score', ascending=False)
~\Anaconda3\lib\site-packages\pandas\core\frame.py in __setitem__(self, key, value)
3368 else:
3369 # set column
-> 3370 self._set_item(key, value)
3371
3372 def _setitem_slice(self, key, value):
~\Anaconda3\lib\site-packages\pandas\core\frame.py in _set_item(self, key, value)
3442 """
3443
-> 3444 self._ensure_valid_index(value)
3445 value = self._sanitize_column(key, value)
3446 NDFrame._set_item(self, key, value)
~\Anaconda3\lib\site-packages\pandas\core\frame.py in _ensure_valid_index(self, value)
3424 value = Series(value)
3425 except (ValueError, NotImplementedError, TypeError):
-> 3426 raise ValueError('Cannot set a frame with no defined index '
3427 'and a value that cannot be converted to a '
3428 'Series')
ValueError: Cannot set a frame with no defined index and a value that cannot be converted to a Series
最佳答案
It seems I have entered wrong inputs, hence the reason I had this error. Below is the snapshot of the code results.
These are case sensitive inputs. That was the reason why I had this issue.
关于python - Pandas 值(value)错误: Cannot set a frame with no defined index and a value that cannot be converted to a Series,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60014108/
最近,我在 XCode 上的调试器停止正常工作,并给出了以下错误: 此帧内的前一帧(gdb 无法展开超过此帧) 我已经浏览过 SO 的另一个线程,谈论丢失的符号。我通过删除符号目录并让它从 iPod
当我在设备上遇到断点时,出现以下调试器错误: 来自调试器的错误:此帧内的上一帧(gdb 无法展开超过此帧) 当应用程序遇到断点时,就会发生这种情况。如果我点击调试器中的继续按钮,它会愉快地继续,直到下
如何在 WinRT XAML 应用程序中操作框架的历史记录? 用户将从我的中心页面开始,他们可以在其中选择一个现有项目以转到其编辑屏幕,或者他们可以选择“新项目”。 “新项目”将引导他们完成一个简短的
2 帧,我希望第 2 帧“覆盖”/更新第 1 帧。基本上,其中 Table1-colB-value = Table2-oldB-value,用 Table2-newB-value 覆盖 Table1-
我正在尝试逐帧动画,但它给了我一个强制关闭,我不确定为什么它给我一个强制关闭。在我看来一切都很好。 这是我的代码,我希望有人能帮忙吗?提前致谢。 动画测试.java import android.ap
这是我的主课。 package pomsystem; public class POMSystem { public static void main(String[] args) {
如果为TRUE,我想从函数返回一个data.frame,否则使用return(ifelse(condition, mydf, NA)) 返回NA 但是, ifelse 会从 data.frame 中删
我正在处理两个 csv 文件并导入为数据框 df1 和 df2 df1 有 50000 行,df2 有 150000 行。 我想比较(遍历每一行)df2 的“时间”df1,求时间差,返回所有列的值对应
我将许多文件夹中的小文本文件读取到一个列表中。因此,我有一个长度为 n 的列表,其中包含 2 个 data.frames。 这是列表元素 3 的示例(在问题末尾输入) ip_list[[3]] $`d
为了找出数据框 df.a 是否是数据框 df.b 的子集,我做了以下操作: df.a semi_join(df.b, df.a) Joining by: c("x", "y") x y 1 1
在某些情况下,出于实现原因,我有一个只包含一个列的 data.frame df=as.data.frame(alpha=1:15) 如果我现在使用 df[-1, ] 它返回一个向量,但我想将它保留为一
Windows 8.1 商店应用中的 Frame.Navigate() 和 this.Frame.Navigate() 有什么区别? 这有什么区别 Frame.Navigate(typeof(Logi
我有两个 data.frames (df1, df2),我想用 df1$V2 的值替换 P1-P10 列中的字母值,但保留前两列df2. df1 = data.frame(V1=LETTERS, V2
有没有更好的方法来制作与现有 data.frame 具有相同维度、列名和行名的空白 data.frame? BAM<-read.table(~/myfile) # 10 rows and 10 co
我有一个列表 data.frame s。例如 set.seed(1) my_list result.df id var_p var_m var_d var_a
假设我有两个数据框 df1 和 df2 如下 Df1 Id Price Profit Month 10 5 2 1 10 5 3 2 10 5 2
我将创建一个网站,除了它自己的内容之外,它还将链接(在 iframe 中)到世界最大的报纸网站,如纽约时报、金融时报和其他一些网站。 但是我遇到了框架许可的问题。例如,纽约时报向我展示了一个错误 Lo
假设我有一个 data.frame: df x x A 1 10 2 20 3 30 从文档中(参见 ?"[" )您可以找到: If drop=TRUE the result is coer
我有一个想要克隆的现有 data.frame,但没有其中的值。 IE。我希望新框架具有与现有框架相同的列名称和类型。原始帧的大小可能为 GB 量级,因此复制和删除数据似乎不是正确的方法,并且迭代现有列
给定两个在列名称/数据类型方面相同的数据帧,其中某些列唯一标识行,是否有一种有效的函数/方法可以让一个数据帧“更新”另一个数据帧? 例如,在下面的例子中,原始和替换由'Name'和'Id'标识>。 g
我是一名优秀的程序员,十分优秀!