私はこのようなデータフレームを持っています
names = ["Patient 1", "Patient 2", "Patient 3", "Patient 4", "Patient 5", "Patient 6", "Patient 7"]
categories = ["Internal medicine, Gastroenterology", "Internal medicine, General Med, Endocrinology", "Pediatrics, Medical genetics, Laboratory medicine", "Internal medicine", "Endocrinology", "Pediatrics", "General Med, Laboratory medicine"]
zippedList = list(zip(names, categories))
df = pd.DataFrame(zippedList, columns=['names', 'categories'])
降伏:
print(df)
names categories
0 Patient 1 Internal medicine, Gastroenterology
1 Patient 2 Internal medicine, General Med, Endocrinology
2 Patient 3 Pediatrics, Medical genetics, Laboratory medicine
3 Patient 4 Internal medicine
4 Patient 5 Endocrinology
5 Patient 6 Pediatrics
6 Patient 7 General Med, Laboratory medicine
(実際のデータ フレームには 1000 行以上あります)
カテゴリを数えると、次のようになります。
print(df['categories'].str.split(", ").explode().value_counts())
Internal medicine 3
General Med 2
Endocrinology 2
Laboratory medicine 2
Pediatrics 2
Gastroenterology 1
Medical genetics 1
n各中間カテゴリが比例して表されるように、行のランダムなサブサンプルを描画したいと思います。たとえば、13 のカテゴリのうち 3 つ (~23%) が「内科」です。したがって、サブサンプルの約 23% がこのカテゴリを持つ必要があります。各患者が 1 つのカテゴリを持っている場合、これはそれほど難しくありませんが、残念ながら複数のカテゴリを持つことができます (たとえば、患者 3 は 3 つのカテゴリを持っています)。これどうやってするの?