I want to have two columns empname and empjosn, the empjson contains employee history in the json format. I want to convert this empjson column to normal data with all the details in a separate column along with empname column.
When I try this code I getting output only for one row, I want this to be for all the values.
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}] |
Required Output:
| 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 |
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']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.