x | Unique_in_x |
---|---|
5,5,6,7,8,6,8 | 4 |
5,9,8,0 | 4 |
5,9,8,0 | 4 |
3,2 | 2 |
5,5,6,7,8,6,8 | 4 |
Unique_in_x is my expected column.Sometime x column might be string also.
You can find the unique value of the list with np.unique()
and then just use the length
import pandas as pd
import numpy as np
df['Unique_in_x'] = df['X'].apply(lambda x : len(np.unique(x.split(','))))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.