Home > Mobile >  How to concatenate/replace specific words and a values in pandas dataframe column
How to concatenate/replace specific words and a values in pandas dataframe column

Time:06-14

I have a column of lists in pandas DataFrame like:

column_name_1
{"deltaImp":"10.3-13.1-14.1-15.1"}
{"deltaImp":"10.3-13.2-14.1-15.1"}

How can I transform the column values so the result should be:

column_name_1
{deltaimp=[{name=10, value=2}, {name=13, value=1}, {name=14, value=1}, {name=15, value=1}]}
{deltaimp=[{name=10, value=2}, {name=13, value=2}, {name=14, value=1}, {name=15, value=1}]}

I did try next steps:

for key, value in pd.Series(df['cookies(babydriver)'].values).items():
    for key, value in dict.items():
        list = [[int(x) for x in ss.split('.')] for ss in value.split('-')]

So I got:

[[10, 3], [13, 2], [14, 1], [15, 1]]

and here I am stuck trying to add "name=" and "value="

The result of df['column_name_1'].head().to_dict() the original contains more values in the same format

{0: '{"deltaImp":"10.3-13.1-14.1-15.1"}',
 1: '{"deltaImp":"10.3-13.2-14.1-15.1"}'}

CodePudding user response:

Just do it while processing the data the first time:

import ast  # built-in


def transformer(col):
    data = ast.literal_eval(col).get("deltaImp", "")
    result = ", ".join("{{name={}, value={}}}".format(*item.split(".")) for item in data.split("-"))
    return f"{{deltaImp=[{result}]}}"

Then:

df["to_DB"] = df["column_name_1"].apply(transformer)

Output:

In [4]: df["to_DB"] = df["column_name_1"].apply(transformer)

In [5]: df
Out[5]:
                        column_name_1                                              to_DB
0  {"deltaImp":"10.3-13.1-14.1-15.1"}  {deltaImp=[{name=10, value=3}, {name=13, value...
1  {"deltaImp":"10.3-13.2-14.1-15.1"}  {deltaImp=[{name=10, value=3}, {name=13, value...

CodePudding user response:

You can use:

df['col2'] = df['col1'].apply(lambda x: [{'name': x[0], 'value': x[1]} for i in x])
print(df)

# Output
                                   col1                                               col2
0  [[10, 3], [13, 2], [14, 1], [15, 1]]  [{'name': [10, 3], 'value': [13, 2]}, {'name':...
1  [[10, 3], [13, 3], [14, 1], [15, 2]]  [{'name': [10, 3], 'value': [13, 3]}, {'name':...

Input data:

>>> df
                                   col1
0  [[10, 3], [13, 2], [14, 1], [15, 1]]
1  [[10, 3], [13, 3], [14, 1], [15, 2]]

CodePudding user response:

You could try:

df.column_name_1.apply(lambda x: {key:[dict(zip(['name', 'value'], map(int, j.split('.')))) for j in val.split('-')] for key, val in eval(x).items()})
  • Related