목록 형식으로 구성된 데이터 프레임 열의 값을 다시 코딩하려고합니다. 데이터 프레임 열의 문자열 값을 대체하는 방법을 알고 있지만 목록에서이를 수행하는 방법에 어려움을 겪고 있습니다.데이터 프레임 열의 목록 값 재 코딩
{0: '[Crime, Drama]',
1: '[Crime, Drama]',
2: '[Crime, Drama]',
3: '[Action, Crime, Drama, Thriller]',
4: '[Crime, Drama]',
5: '[Biography, Drama, History]',
6: '[Crime, Drama]',
7: '[Adventure, Drama, Fantasy]',
8: '[Western]',
9: '[Drama]'}
예를 들어, 내가 역사에 스릴러와 전기에 대한 모든 범죄를 코딩하고 싶습니다 : 여기
내 데이터의 조각이다. 나는 문자열 교체를 위해 아래 작품df.loc[df['genre']=='Crime']='Thriller'
가치를 알고 있지만 어떻게 목록이 수정합니까?
감사합니다.
EDIT (IMDB 데이터베이스로부터 추출 된 데이터)이 dataframe를 만드는 데 사용되는 코드이다 :
# these are the variables we want to (ie are able to) extract from the movie object
metadata = ('title', 'rating', 'genre', "plot", "language", "runtime", "year", "color", "country" , "votes")
#creates dataframe with variable name headers
df = pd.DataFrame(np.random.randn(250, len(metadata)), columns=metadata)
#these are all different data types, including lists, this makes it compile
df = df.astype('object')
#populate df with movie objects
for i in range(250):
for j in metadata:
df.loc[i, j] = movies_list[i].get(j)
# convert to the right data types:
metadata_dict_dtypes = {"title": unicode,
"rating": float,
"genre":list,
"plot": str,
"language":list,
"runtime":list,
"year":int,
"color":list,
"country":list ,
"votes":int}
for colname, my_dtype in metadata_dict_dtypes.iteritems():
df[colname] = df[colname].astype(my_dtype)
do 0-9는 데이터 프레임의 다른 열 또는 행을 나타 냅니까? –
행이 다릅니다. – yogz123
'[Crime, Drama]'(괄호 안의 괄호 안의 바깥 쪽 따옴표)는 목록이 아닌 문자열처럼 보입니다. 실제로이 데이터 프레임을 생성하는 코드를 보여줄 수 있으므로 작업 한 내용을 정확히 알고 있습니까? –