Home > Mobile >  How to cast type of one column's values from another column which contains 'type'?
How to cast type of one column's values from another column which contains 'type'?

Time:01-14

What my df looks like -

data = [("A", "2.1", "float"), ("B", "-2.0", "float"), ("C", "5.45", "int"), ("D", "John", "str"), ("E", "['US', 'UE']", "list"), ("F", "{'alive': True}", "dict")]

df = pd.DataFrame(data, columns=['vname','value','type'])
print(df)

    vname            value  type
0     A              2.1    float
1     B               -2    float
2     C              5.45   int
3     D             John    str
4     E         [US, UE]    list
5     F  {'alive': True}    dict

df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 6 entries, 0 to 5
Data columns (total 3 columns):
 #   Column  Non-Null Count  Dtype 
---  ------  --------------  ----- 
 0   vname   6 non-null      object
 1   value   6 non-null      object
 2   type    6 non-null      object
dtypes: object(3)
memory usage: 272.0  bytes
    

What i want is dictionary like -

{ 'A':2.1, 'B':-2.0, 'C':5, 'D':'John', 'E':['US','UE'], 'F':{'alive': True} }

Note - The problem is that, I have values from the value column with type str

CodePudding user response:

You can use zip function.

>>> import pandas as pd
>>> data = [
...     ("A", 2.1, "float"),
...     ("B", -2.0, "float"),
...     ("C", 500, "int"),
...     ("D", "John", "str"),
...     ("E", ["US", "UE"], "list"),
...     ("F", {"alive": True}, "dict"),
... ]
>>> 
>>> df = pd.DataFrame(data, columns=["vname", "value", "type"])
>>> my_dict = dict(zip(df["vname"], df["value"]))
>>> print(type(my_dict["A"]))
<class 'float'>
>>> print(type(my_dict["B"]))
<class 'float'>
>>> print(type(my_dict["D"]))
<class 'str'>
>>> print(type(my_dict["E"]))
<class 'list'>
>>> print(type(my_dict["F"]))
<class 'dict'>

CodePudding user response:

Solution

import pandas as pd

data = [('A', 2.1, 'float'), ('B', -2.0, 'float'), ('C', 500, 'int'), ('D', 'John', 'str'), ('E', ['US', 'UE'], 'list'), ('F', {'alive': True}, 'dict')]

df = pd.DataFrame(data, columns=['vname','value','type'])
print(df)

OUTPUT

  vname  ...   type
0     A  ...  float
1     B  ...  float
2     C  ...    int
3     D  ...    str
4     E  ...   list
5     F  ...   dict

[6 rows x 3 columns]
print(df.info())

OUTPUT

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 6 entries, 0 to 5
Data columns (total 3 columns):
vname    6 non-null object
value    6 non-null object
type     6 non-null object
dtypes: object(3)
memory usage: 272.0  bytes
None
print(dict(zip(df['vname'], df['value'])))

OUTPUT

{'A': 2.1, 'B': -2.0, 'C': 500, 'D': 'John', 'E': ['US', 'UE'], 'F': {'alive': True}}

CodePudding user response:

A simple conversion to_dict should preserve the original types if you really have the DataFrame as provided:

df.set_index('vname')['value'].to_dict()

Output:

{'A': 2.1, 'B': -2.0, 'C': 500, 'D': 'John', 'E': ['US', 'UE'], 'F': {'alive': True}}

If really you have strings in "value" (df['value'] = df['value'].astype(str)) you can convert using:

from ast import literal_eval

values = (df.groupby('type', group_keys=False)['value']
            .apply(lambda g: g.apply(literal_eval)
                             if g.name in ['list', 'dict']
                             else g.astype(g.name))
         )

out = dict(zip(df['vname'], values))
  • Related