在数据框中将列拆分为多列

时间:2019-05-09 05:03:16

标签: python dataframe dictionary

我已经使用Dictionary创建了一个数据框,现在我想将数据框中的2列拆分为4列。

数据框中最初有3列,即“父级”,“子级”和“得分”。我想将“父项”列拆分为“ col1和col2”,将“子级”列拆分为“ col3和col4”,并希望使用分隔符'+'对其进行拆分

我尝试了以下某些方法可以提供帮助

def request_service(Sentence,id):
        dict_ip = {"id": "2018 Regression", "Sentence": "What is the customers Issue/problem?  Customer spoke to our mobile banking help
        payload = json.dumps(dict_ip)
        print(payload)

        response = requests.request("POST", url, data=payload, headers=headers)
        dict_list = json.loads(response.text)
        print(dict_list)
        #dict_list = {'results': [{'Parent': 'A+B', 'child': 'C+D', 'score': 0.36283498590263946}, {'Parent': 'D+E', 'child': 'A+B', 'score': 0.10505374311256221}, {'Parent': 'N+M', 'child': 'Q+R', 'score': 0.09593593898873307}]}

        df_op = pd.DataFrame(columns=['Parent', 'Child', 'Score'])
        for idx, result in enumerate(dict_list['results']):
        df_op.loc[idx] = [result['Parent'], result['child'], result['score']]
        df_op.Score = df_op.Score.round(2)
        return df_op

预期输出为5列Datframe

 col1   col2   col3   col4   Score
  A      B      C      D      0.36
  D      E      A      B      0.10
  N      M      Q      R      0.09

1 个答案:

答案 0 :(得分:0)

我找到了一种拆分列的方法

df_op = pd.DataFrame(columns=['Parent', 'Child', 'Score'])
for idx, result in enumerate(dict_list['results']):
    df_op.loc[idx] = [result['Parent'], result['child'], result['score']]
df3 = pd.DataFrame(df_op.Parent.str.split('+', expand=True).values,
         columns=['col1', 'col2'])
df4 = pd.DataFrame(df_op.Child.str.split('+', expand=True).values,
                   columns=['col3', 'col4'])
df_op_mergerd = pd.concat([df3, df4, df_op], axis=1)
df_op_mergerd.drop(['Parent','Child'], axis=1, inplace=True)