분류 할 텍스트 데이터가 있습니다. 개별 문자열을 지정하는 for 루프를 사용하여 특정 단어 나 구가 다른 열의 행에 있는지 식별합니다. true이면 루프가 새 값에 특정 값을 추가합니다. 그런 다음 새 목록이 DataFrame
에 추가됩니다. 그러나이 방법은 수많은 테스트를 위해 수많은 문자열을 지정해야하므로 실제 데이터에는 너무 다루기 힘듭니다.루프에서 각 문자열을 개별적으로 참조하지 않고 for 루프에서 문자열을 검색합니다.
루프가 검색 할 수있는 단일 데이터 구조 내에서 개별 문자열을 그룹화 할 수있는 방법이 있습니까? 루프의 각 테스트가 의미하는 바는 루프 내부에서 철자가 지정된 개별 문자열과 달리 하나의 데이터 구조 만 참조합니다. 이 작업을 수행 할 수 있습니까?
다음은 현재 수행중인 작업의 재현 가능한 예입니다. 이 루프에 대한 다음과 같은 분류를 수행 그리고
opinion
0 He said it was too expensive
1 She said it was too costly
2 He thought it was not fast enough
3 They thought is was not right and too much money
4 Her view was that it was too small and too slow
:
data = {
'opinion': ['He said it was too expensive',
'She said it was too costly',
'He thought it was not fast enough',
'They thought is was not right and too much money',
'Her view was that it was too small and too slow',
]}
df = pd.DataFrame(data, columns = ['opinion'])
df
이를 만듭니다.
new_col=[]
for row in df['opinion']:
if 'too expensive' in row or 'too costly' in row or 'too much money' in row:
new_col.append('Too Expensive')
elif 'not fast enough' in row or 'too slow' in row:
new_col.append('Too Slow')
df['reason'] = new_col
df
opinion reason
0 He said it was too expensive Too Expensive
1 She said it was too costly Too Expensive
2 He thought it was not fast enough Too Slow
3 They thought is was not right and too much money Too Expensive
4 Her view was that it was too small and too slow Too Slow
실제 데이터에는 각 테스트마다 루프 내부에 개별 문자열 점수를 쓸 수는 없지만 실제로는 너무 많습니다.
당신은'너무 Slow'에 대해 잊어 버려. – jezrael
MWE가 충분하다고 생각했습니다. – VBB