df中一列list拆成多列
import numpy as np
import pandas as pd
list1 = [["info1", ["zhangsan", "nan", "27fen"]], ["info2", ["lisi", "nv", "35fen"]],
["info3", ["wangwu", "nan", "45fen"]]]
df = pd.DataFrame(list1, columns=["A", "B"])
df_1 = pd.DataFrame(df["B"].values.tolist(), columns=["C", "D", "E"])
df = pd.concat([df, df_1], axis=1)
print(df)
要把df中的“B”列d拆成3列
C:\Users\phoeb\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/phoeb/PycharmProjects/pythonProject/numpysty.py
A B C D E
0 info1 [zhangsan, nan, 27fen] zhangsan nan 27fen
1 info2 [lisi, nv, 35fen] lisi nv 35fen
2 info3 [wangwu, nan, 45fen] wangwu nan 45fen
Process finished with exit code 0
实测方法2 在数据量大时更有效
result = pd.DataFrame(all_log["mail_attach"].values.tolist(),
columns=['mail_attach_name', 'mail_attach_ordinal', 'mail_attach_name_suffix',
"mail_attach_size"])
result = all_log["mail_attach"].apply(pd.Series, index=['mail_attach_name', 'mail_attach_ordinal',
'mail_attach_name_suffix', "mail_attach_size"])