다음 코드가 있습니다.
df= pd.DataFrame(data=all_r_1.to_dataframe().groupby(['user_id'])['type'].sum()).reset_index()
userid | type
20 | aab
21 | ababb
type
열의 문자열에서 중복을 제거하려면 다음 코드가 있습니다.
df['type'] = df['type'].apply(lambda x: ''.join(ch for ch, _ in itertools.groupby(x)))
다음을 생성합니다.
userid | type
20 | ab
21 | abab
이것은 입력 df입니다.
id | userid | type
1 | 20 | a
2 | 20 | a
3 | 20 | b
4 | 21 | a
5 | 21 | b
6 | 21 | a
7 | 21 | b
8 | 21 | b
그러나 내가하고 싶은 것은 중복을 제거하는 동안 각 문자의 개수를 포함하는 것입니다.
userid | type
20 | a2b
21 | abab2
itertools.groupby
카운트를 포함하도록 코드를 수정하는 방법에 대한 아이디어가 있습니까?
itertools.groupby
다음과 같이 액세스 할 수 있도록 실제 그룹을 저장합니다.
df['type'] = df['type'].apply(lambda x: ''.join('{}{}'.format(ch,len(list(group))) for ch, group in itertools.groupby(x)))
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다