如何将具有 [1](对象)等值的列转换为数值?
How can I convert a column with values like [1] (objects) to a numeric value?
我想将以下列(DataFrame 的一部分)转换为数值。
df = pd.DataFrame({ "Cluster": [[0], [1], [0], [2]]})
我的以下代码不起作用:
pd['Cluster_numeric'] = pd.to_numeric(df['Cluster'], errors='coerce')
考虑到您的列表长度始终为 1,这应该可行
df['Cluster_numeric']=df['Cluster'].apply(lambda x: x[0])
我想将以下列(DataFrame 的一部分)转换为数值。
df = pd.DataFrame({ "Cluster": [[0], [1], [0], [2]]})
我的以下代码不起作用:
pd['Cluster_numeric'] = pd.to_numeric(df['Cluster'], errors='coerce')
考虑到您的列表长度始终为 1,这应该可行
df['Cluster_numeric']=df['Cluster'].apply(lambda x: x[0])