json到pandas数据框,第一列的值在所有行中都相同

时间:2020-09-09 15:58:24

标签: python json python-3.x dataframe

我想有两列empname和empjosn,empjson包含json格式的员工历史记录。我想将此empjson列转换为普通数据,并将所有详细信息与empname列一起放在单独的列中。

当我尝试这段代码时,我只得到一行的输出,我希望这是所有值的输出。

df[['alias','deptid','empid','mgnme','salary']] = pd.DataFrame.from_records(df['empjson'][0])

DataFrame:

| empname | empjson |
| David   | [{'alias': 'xxxxx', 'deptid':   'P1021', 'empid': 10749, 'mgnme': 'hhhhh', 'salary': 20123}, {'alias':   'xxxxx', 'deptid': 'P1021', 'empid': 10749, 'mgnme': 'hhhhh', 'salary':   20123}, {'alias': 'xxxxx', 'deptid': 'P1021', 'empid': 10749, 'mgnme':   'hhhhh', 'salary': 20123}, {'alias': 'xxxxx', 'deptid': 'P1021', 'empid':   10749, 'mgnme': 'hhhhh', 'salary': 20123}, {'alias': 'xxxxx', 'deptid':   'P1021', 'empid': 10749, 'mgnme': 'hhhhh', 'salary': 20123}, {'alias':   'xxxxx', 'deptid': 'P1021', 'empid': 10749, 'mgnme': 'hhhhh', 'salary':   20123}, {'alias': 'xxxxx', 'deptid': 'P1021', 'empid': 10749, 'mgnme':   'hhhhh', 'salary': 20123}, {'alias': 'xxxxx', 'deptid': 'P1021', 'empid':   10749, 'mgnme': 'hhhhh', 'salary': 20123}, {'alias': 'xxxxx', 'deptid':   'P1021', 'empid': 10749, 'mgnme': 'hhhhh', 'salary': 20123}, {'alias':   'xxxxx', 'deptid': 'P1021', 'empid': 10749, 'mgnme': 'hhhhh', 'salary':   20123}, {'alias': 'xxxxx', 'deptid': 'P1021', 'empid': 10749, 'mgnme':   'hhhhh', 'salary': 20123}, {'alias': 'xxxxx', 'deptid': 'P1021', 'empid':   10749, 'mgnme': 'hhhhh', 'salary': 20123}, {'alias': 'xxxxx', 'deptid':   'P1021', 'empid': 10749, 'mgnme': 'hhhhh', 'salary': 20123}, {'alias':   'xxxxx', 'deptid': 'P1021', 'empid': 10749, 'mgnme': 'hhhhh', 'salary':   20123}, {'alias': 'xxxxx', 'deptid': 'P1021', 'empid': 10749, 'mgnme':   'hhhhh', 'salary': 20123}, {'alias': 'xxxxx', 'deptid': 'P1021', 'empid':   10749, 'mgnme': 'hhhhh', 'salary': 20123}, {'alias': 'xxxxx', 'deptid':   'P1021', 'empid': 1829, 'mgnme': 'hhhhh', 'salary': 1061}] |

必需的输出:

| empname | alias | deptid | empid | mgnme | salary |
| David   | xxxxx | P1021  | 10749 | yyyyy | 20123  |
| David   | xxxxx | P1021  | 10749 | yyyyy | 20123  |
| David   | xxxxx | P1021  | 10749 | yyyyy | 20123  |
| David   | xxxxx | P1021  | 10749 | yyyyy | 20123  |
| David   | xxxxx | P1021  | 10749 | yyyyy | 20123  |
| David   | xxxxx | P1021  | 10749 | yyyyy | 20123  |
| David   | xxxxx | P1021  | 10749 | yyyyy | 20123  |
| David   | xxxxx | P1021  | 10749 | yyyyy | 20123  |
| David   | xxxxx | P1021  | 10749 | yyyyy | 20123  |
| David   | xxxxx | P1021  | 10749 | yyyyy | 20123  |
| David   | xxxxx | P1021  | 10749 | yyyyy | 20123  |
| David   | xxxxx | P1021  | 10749 | yyyyy | 20123  |
| David   | xxxxx | P1021  | 10749 | yyyyy | 20123  |
| David   | xxxxx | P1021  | 10749 | yyyyy | 20123  |
| David   | xxxxx | P1021  | 10749 | yyyyy | 20123  |
| David   | xxxxx | P1021  | 10749 | yyyyy | 20123  |
| David   | xxxxx | P1021  | 10749 | yyyyy | 20123  |

1 个答案:

答案 0 :(得分:1)

import pandas as pd

df = pd.read_csv(r'read_file.csv')

js =df.iloc[0,1].replace("[{","").replace("}]","").split("}, {")

lst = []
for i in range(len(js)):
    lt = []
    dict = eval("{" + js[i] + "}")
    lt.append(df.iloc[0,0])
    lt.append(dict['alias'])
    lt.append(dict['deptid'])
    lt.append(dict['empid'])
    lt.append(dict['mgnme'])
    lt.append(dict['salary'])
    lst.append(lt)
    
final = pd.DataFrame(lst)
final.columns = ['name','alias', 'deptid', 'empid', 'mgnme', 'salary']