[英]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.