3 回答
TA贡献1786条经验 获得超11个赞
您可以使用apply当前列表检查:
import pandas as pd
import random
# create random df
x = [{'A': [random.randint(0,300) for i in range(10)]} for i in range(10)]
df = pd.DataFrame(x)
df.A.apply(lambda x: all(isinstance(y, int) for y in x))
0 True
1 True
2 True
3 True
4 True
5 True
6 True
7 True
8 True
9 True
# add non-int row
x = [{'A': [random.randint(0,300) for i in range(10)]} for i in range(10)] + [{'A':[chr(a) for a in range(100,120)]}]
df = pd.DataFrame(x)
df.A.apply(lambda x: all(isinstance(y, int) for y in x))
0 True
1 True
2 True
3 True
4 True
5 True
6 True
7 True
8 True
9 True
10 False
Name: A, dtype: bool
TA贡献1804条经验 获得超7个赞
创建一个函数并应用它应该可以工作:
def check_list(lst):
return all(isinstance(x, int) for x in lst)
df['is_all_ints'] = df['Field1'].apply(check_list)
TA贡献1874条经验 获得超12个赞
callable你可以像这样传递一个.loc:
df = pd.DataFrame({
"a": [[1, 2, 3], [1, 1, 1], [3, 2, 1], [1, 2, "a"]],
"b": ["a", "b", "c", "d"]
})
df.loc[
lambda df: df["a"].apply(
lambda lst: all(isinstance(item, int) for item in lst)
)
]
结果:
a b
0 [1, 2, 3] a
1 [1, 1, 1] b
2 [3, 2, 1] c
添加回答
举报