【发布时间】:2018-09-19 04:15:28
【问题描述】:
这是我的 CSV:
languages, origin, other_test1, other_test2
"[{'name': 'French', 'vowel_count': 3}, {'name': 'Dutch', 'vowel_count': 4}, {'name': 'English', 'vowel_count': 5}]",Germanic,ABC,DEF
我想将 CSV 的语言列转换为以下输出:
Language_name ,Language_vowel_count, origin, other.test1, other.test2
French, 3, Germanic, ABC, DEF
Dutch, 4, Germanic, ABC, DEF
English, 5, Germanic, ABC, DEF
我尝试过的代码:
from itertools import chain
a = df['languages'].str.findall("'(.*?)'").astype(np.object)
lens = a.str.len()
df = pd.DataFrame({
'origin' : df['origin'].repeat(lens),
'other_test1' : df['other_test1'].repeat(lens),
'other_test2' : df['other_test2'].repeat(lens),
'name' : list(chain.from_iterable(a.tolist())),
'vowel_count' : list(chain.from_iterable(a.tolist())),
})
df
但它没有给我预期的输出。
【问题讨论】:
标签: python pandas list dataframe itertools