import pandas as pd
my_list = [[1, 'ABC', {'link':'http://smthing1'}], # list
[2, 'DEF', {'link':'http://smthing2'}]]
df = pd.DataFrame(my_list, columns=['col1', 'col2', 'col3']) #convert to pandas dataframe
convert_col3_to_dict = dict(df['col3']) #convert string to dict
list1 = []
list2 = []
for i in range(len(my_list)):
elements = my_list[i][:2]
#remove empty list
elements = [x for x in elements if x != []]
# print(elements)
list1.append(elements)
for key, val in convert_col3_to_dict.items(): #Iterating through the col3
for k,v in val.items(): #iterating through values in col3
# print([v]) # printing the value
list2.append([v])
new_list = []
for f, b in zip(list1, list2):
# print(f, b)
tem_list = f + b
new_list.append(tem_list)
new_df = pd.DataFrame(new_list, columns=['col1', 'clo2', 'col3'])
print(new_df)
使用^{} ,但如有必要,首先转换为字典:
如果您喜欢长代码:
结果:
相关问题 更多 >
编程相关推荐