output = [len(x) if isinstance(x, list) else 0 for x in df['column']]
下面是一个使用您的输入的示例
import pandas as pd
import numpy as np
df = pd.DataFrame({'column': [['a','b'], np.nan, []]})
output = [len(x) if isinstance(x, list) else 0 for x in df['column']]
print(output)
2条答案
按热度按时间ht4b089n1#
smdncfj32#
您可以检查项目是否为list:
*如果是列表-确定该列表的长度
*如果不是列表(例如np.nan)-则设置为零。
下面是一个使用您的输入的示例
输出: