简体   繁体   English

如何在数据框列中找到不同数量的元素,其中字符串包含由分号分隔的多个元素

[英]How to find the distinct number of elements in data frame column, in which strings contain multiple elements separated by a semi-colon

I'm importing some data from excel and trying to make a dashboard on streamlit.我正在从 excel 导入一些数据并尝试在 Streamlit 上制作一个仪表板。 Right now, I'm trying to count the number of distinct elements in one of the columns of a data frame referred to as 'Tags'.现在,我正在尝试计算称为“标签”的数据框列之一中不同元素的数量。 However, for some of the rows, I have distinct values that are combined into a single string, rather than multiple strings.但是,对于某些行,我将不同的值组合成一个字符串,而不是多个字符串。

With the first 'for loop' the data came out like this... "Python; C++" "Java; Python" "R; C; Java"第一个“for循环”的数据是这样出来的......“Python; C++”“Java;Python”“R;C;Java”

Instead of like... [Python, C++, Java, R, C].而不是像... [Python, C++, Java, R, C]。 With the second 'for loop,' I'm attempting to do what I want, however, the program outputs nothing.对于第二个“for 循环”,我正在尝试做我想做的事情,但是,该程序没有输出任何内容。 What am I doing wrong?我究竟做错了什么?

cnt=0
visited=[]
for i in range(0, len(df1['Tags'])):
    
    if df1['Tags'][i] not in visited: 
        
        visited.append(df1['Tags'][i])
          
        cnt += 1
u=[]
for j in range(0, len(visited)):
    new= visited[j].split(';')
    for z in range(0, len(new)):
        if new not in u:
            u.append(new)
st.write(new)

是你想要的结果吗?

list(set([j.strip() for i in df1["Tags"] for j in i.split(';')]))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DataFrame 在每列中有多个分号分隔的变量。 如何一键编码? - DataFrame with multiple semi-colon separated variables in each column. How to one-hot encode? 在熊猫数据框中按具有半冒号分隔值的列分组 - Group by a column with semi colon separated values in pandas data frame Python写输出用分号分隔 - Python write output separated by semi-colon 正则表达式用于读取用分号分隔的字符串并捕获特殊字符。 蟒蛇 - Regex for reading strings separated by semi-colon and capturing special characters. Python 如何查找存储在 pandas 数据框列中的逗号分隔字符串中唯一值的数量? - How to find the number of unique values in comma separated strings stored in an pandas data frame column? 如何使用REGEX从字符串中替换分号的确切数字? - How to substitute exact number of semi-colon from string using REGEX? 如何在pyspark中将字符串分号分隔的列转换为MapType? - How to convert string semi colon-separated column to MapType in pyspark? 通过单元格而不是用python分号将数据保存在excel中 - Saving data in excel by cell rather than semi-colon with python 如何找到嵌入在 Pandas 数据框列中的元素列表的平均值 - How to find the average of a list of elements imbedded in a Pandas data frame column 可靠地转换以逗号/分号分隔的“姓,名,姓,名”列表 - Reliably convert comma/semi-colon separated list of “last, first, last, first,” names
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM