【发布时间】:2020-12-31 23:22:37
【问题描述】:
我有一个 csv 表,其中有一列(标签)充满了字符串列表。要将其转换为我使用的 pd 系列
def flatten(series):
return pd.Series(series.dropna().sum())
tags_sorted = flatten(df['tags'])
现在我想在其中一个列表中搜索系列中的字符串,以便它返回该字符串在列中出现的次数。我找到了这个功能:
def find(series, tag):
for i in series.index:
if series[i] == tag:
return i
return None
并在我的系列中使用它:
print(find(tags_sorted, 'romance'))
但它不断返回None,即使该字符串肯定在多个列表中。
我也试过
print(tags_sorted[tags_sorted == "romance"])
和
print(tags_sorted.loc[tags_sorted == 'romance'])
但那些只返回[]。
【问题讨论】:
标签: python pandas string series