Python 带有拆分字符串的dataframe.loc
如何使用split匹配列中转换为str的intPython 带有拆分字符串的dataframe.loc,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,如何使用split匹配列中转换为str的int personid = '23' cond = (personid == x for x in df['wait'].str.split(",")) rows = df.loc[cond, :] df['wait']包含类似“2,13,23”的str 当前,如果addexpand=True通过拆分的值获取具有新列fileld的数据帧,则返回空数据帧,然后通过进行比较,并通过测试每行是否至少有一个True。最后筛选人: 如果要匹配
personid = '23'
cond = (personid == x for x in df['wait'].str.split(","))
rows = df.loc[cond, :]
df['wait']
包含类似“2,13,23”的str
当前,如果add
expand=True
通过拆分的值获取具有新列fileld的数据帧,则返回空数据帧
,然后通过进行比较,并通过测试每行是否至少有一个True
。最后筛选人:
如果要匹配personid
中的整数值:
#personid is integer
personid = 23
cond = df['wait'].str.split(",", expand=True).astype(float).eq(personid).any(axis=1)
rows = df[cond]
您的解决方案应该更改:
cond = [any(y == personid for y in x) for x in df['wait'].str.split(",")]
如果add
expand=True
以通过拆分的值获取具有新列fileld的数据帧,则按进行比较,并测试每行是否至少有一个True
。最后筛选人:
如果要匹配personid
中的整数值:
#personid is integer
personid = 23
cond = df['wait'].str.split(",", expand=True).astype(float).eq(personid).any(axis=1)
rows = df[cond]
您的解决方案应该更改:
cond = [any(y == personid for y in x) for x in df['wait'].str.split(",")]
请使用预期输出发布样本输入数据。请使用预期输出发布样本输入数据。此操作有效。你能解释一下语法吗?匹配的整数不会使用
int
而不是float
?这很有效。你能解释一下语法吗?匹配的整数不是使用int
而不是float
?