我正在使用python 3.7。我试图将数据合并到一个通用数据集中,然后创建一个数据透视表并找到一部电影与该数据集之间的相关性。我试图重命名获得的相关列,并在相关数据框中搜索特定值。我在符号标题上遇到错误
import pandas as pd
import numpy as np
import warnings
import matplotlib.pyplot as plt
import seaborn as sns
warnings.filterwarnings('ignore')
#reading data file u.data
df = pd.read_csv('u.data', sep='\t', names=['user_id','item_id','rating','titmestamp'])
#reading csv file movies.csv
movie_titles = pd.read_csv('movies.csv',names=['item_id','title','genre'])
#merging the two data sets on field item_id
df = pd.merge(df, movie_titles, on='item_id')
#grouping ratings by title
ratings = pd.DataFrame(df.groupby('title')['rating'].mean())
ratings['number_of_ratings'] = df.groupby('title')['rating'].count()
ratings.sort_values('number_of_ratings', ascending=False)
movie_matrix = df.pivot_table(index='user_id', columns='title', values='rating')
AFO_user_rating = movie_matrix['Air Force One (1997)']
contact_user_rating = movie_matrix['Contact (1997)']
similar_to_air_force_one=movie_matrix.corrwith(AFO_user_rating)
abc=similar_to_air_force_one.to_frame()
#Now I have tried different combinations like
abc.rename(columns={"title":"t","0":"c"})
#and
abc.columns=['t','v']
#but none of them are working. when I try this
#the second columns title value gets changed
abc.columns=['t']
#when i print the column t whole frame gets printed
print(abc[['t']])
#I want to search the frame for a value> I tried different methods like
print(abc[abc[[0]]=='Til There Was You (1997)']['1'])
#and this. In this I am getting error on the symbol title
print(abc.loc(similar_to_air_force_one['title'].str.find('Til There Was You (1997)')))
# but none of them works.
Sample movies.csv
1 Toy Story (1995) Adventure|Animation|Children|Comedy|Fantasy
2 Jumanji (1995) Adventure|Children|Fantasy
sample u.data
196 242 3 881250949
186 302 3 891717742
22 377 1 878887116