두 개의 데이터 프레임이 있는데, df는 튜플 test_df
이었지만 목록 product_combos
이었습니다. 나는 test_df
다음과 같이 튜플로 변경했습니다 .
[in] print(testing_df.head(n=5))
[out]
product_id
transaction_id
001 [P01]
002 [P01, P02]
003 [P01, P02, P09]
004 [P01, P03]
005 [P01, P03, P05]
[in] print(product_combos1.head(n=5))
[out]
product_id count length
0 (P06, P09) 36340 2
1 (P01, P05, P06, P09) 10085 4
2 (P01, P06) 36337 2
3 (P01, P09) 49897 2
4 (P02, P09) 11573 2
# Convert the lists to tuples
testing_df1 = testing_df['product_id'].apply(tuple)
이제 길이 열을 test_df1
(각 행의 문자열 수를 계산하는)에 추가하려고 할 때 문제가 발생 합니다.
먼저 길이 열을 추가 한 다음 튜플로 변환하려고 시도했지만 이것을 시도하면 길이 열이 사라집니다. 나는 또한했다 :
testing_df1['length'] = testing_df['product_id'].str.len()
그러나 이것은 단지 말도 안되는 행을 추가합니다. 나는 또한 시도했다 :
testing_df1['length'] = testing_df['product_id'].apply(len)
이것도 작동하지 않는 것 같습니다. 내가 뭘 잘못하고 있으며 어떻게 고칠 수 있습니까?
잘 작동한다
df = pd.DataFrame([[1,['a','b']],[2,['a','b','c']],[3,['c','b']],[4,['b','d']],[5,['c','a']]])
df :
0 1
0 1 [a,b]
1 2 [a, b, c]
2 3 [c, b]
3 4 [b, d]
4 5 [c, a]
df[1] = df[1].apply(tuple)
df['length'] = df[1].apply(len)
df :
0 1 length
0 1 (a, b) 2
1 2 (a, b, c) 3
2 3 (c, b) 2
3 4 (b, d) 2
4 5 (c, a) 2
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다