用标签conn构建数据帧

2024-10-05 11:46:52 发布

您现在位置:Python中文网/ 问答频道 /正文

如何添加目标列中的字符串计数。你知道吗

data = [{'target': ['Aging','Brain', 'Neurons', 'Genetics']}, 
        {'target': ['Dementia', 'Genetics']}, 
        {'target': ['Brain','Dementia', 'Genetics']}]

df = pd.DataFrame(data)

数据帧

target
0   [Aging, Brain, Neurons, Genetics]
1   [Dementia, Genetics]
2   [Brain, Dementia, Genetics]

唯一标签

target = []
for sublist in df['target'].values:
    tmp_list = [x.strip() for x in sublist]
    target.extend(tmp_list)

target = list(set(target))

# ['Brain', 'Neurons', 'Aging', 'Genetics', 'Dementia']

预期输出在这里 enter image description here


Tags: 字符串intarget目标dffordatatmp
2条回答

也许这会有帮助

# Instead of creation of target list ,
# Convert list of str to one single str 
list_to_str = [" ".join(tags['target']) for tags in data]

##
#['Aging Brain Neurons Genetics',
# 'Dementia Genetics',
# 'Brain Dementia Genetics',
# 'Neurons Brain Neurons Neurons'
# ]

# Using CountVector
from sklearn.feature_extraction.text import CountVectorizer
text_data = np.array(list_to_str)

# Create the bag of words feature matrix
count = CountVectorizer()
bag_of_words = count.fit_transform(text_data)   # needs to coverted to array

# Get feature names
feature_names = count.get_feature_names()

# Create df
df1  = pd.DataFrame(bag_of_words.toarray(), columns=feature_names)

print(df1)

## Output
   aging  brain  dementia  genetics  neurons
0      1      1         0         1        1
1      0      0         1         1        0
2      0      1         1         1        0
3      0      1         0         0        3

如果需要指示符列(仅01):

使用^{}

from sklearn.preprocessing import MultiLabelBinarizer

mlb = MultiLabelBinarizer()
df1 = pd.DataFrame(mlb.fit_transform(df['target']),columns=mlb.classes_)
print (df1)
   Aging  Brain  Dementia  Genetics  Neurons
0      1      1         0         1        1
1      0      0         1         1        0
2      0      1         1         1        0

或者^{}^{}-但是它更慢:

df1 = df['target'].str.join('|').str.get_dummies()

如果需要列表中的计数值:

data = [{'target': ['Neurons','Brain', 'Neurons', 'Neurons']}, 
        {'target': ['Dementia', 'Genetics']}, 
        {'target': ['Brain','Brain', 'Genetics']}]

df = pd.DataFrame(data)

from collections import Counter
df = pd.DataFrame([Counter(x) for x in df['target']]).fillna(0).astype(int)
print (df)

   Brain  Dementia  Genetics  Neurons
0      1         0         0        3
1      0         1         1        0
2      2         0         1        0

相关问题 更多 >

    热门问题