如何将所有字符串(例如" Fault")转换为独特的浮点数?

时间:2017-02-25 16:42:43

标签: python pandas numpy keras

我有一个DataFrame,其中包含intfloatobject(带字符的字符串)项。我希望每个独特的对象都有一个独特的浮点数 -

Exhuast
Fault
Probation
Exhaust
Fault
Motor

1.
2.
3.
1.
2.
4.

此外,它是否适用于所有列,还是我必须逐列?

最后一个问题,它是否也会将所有int转换为float

1 个答案:

答案 0 :(得分:4)

如Jon所述,您可以使用Series.factorize

def find_paths_from_standard_user_to_domain_admins(standard_user, domain_admins_group):
    """Returns a list of paths that a standard user could take to get domain admin credentials"""
    results = []
    query = "MATCH (u:User {name:{user}}), (target:Group {name: {group}})," \
            "p=allShortestPaths((u)-[*]->(target)) RETURN p"
    with driver.session() as session:
        with session.begin_transaction() as tx:
            for record in tx.run(query, user=standard_user, group=domain_admins_group):
                relationships = record["p"].relationships
                nodes = record["p"].nodes
                path = ""
                for i in (range(len(relationships))):
                    path += "{0}-[{1}]->".format(nodes[i]["name"], relationships[i].type)
                path += nodes[-1]["name"]
                results.append(path)
    return results

要在整个DataFrame上按列执行此操作,只需使用(s.factorize()[0]+1).astype('float') 即可。

<强>演示

apply

NumPy解决方案可能是使用>>> s = pd.Series(['Exhaust', 'Fault', 'Probation', 5, int, 'Exhaust', int, 'Fault', 'Motor']) >>> s 0 Exhaust 1 Fault 2 Probation 3 5 4 <class 'int'> 5 Exhaust 6 <class 'int'> 7 Fault 8 Motor dtype: object >>> (s.factorize()[0]+1).astype('float') array([ 1., 2., 3., 4., 5., 1., 5., 2., 6.]) 的{​​{1}}关键字arg,

return_inverse
然而,从一些粗略的基准测试来看,Pandas解决方案可能会更快。