本文介绍了在 pandas 数据框中将列表元素拆分为子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据框为:-
Filtered_data
['defence possessed russia china','factors driving china modernise']
['force bolster pentagon','strike capabilities pentagon congress detailing china']
[missiles warheads', 'deterrent face continued advances']
......
......
我只想将每个列表元素拆分为子元素(标记词).因此,输出Im寻找为:-
I just want to split each list elements into sub-elements(tokenized words).So, output Im looking for as:-
Filtered_data
[defence, possessed,russia,factors,driving,china,modernise]
[force,bolster,strike,capabilities,pentagon,congress,detailing,china]
[missiles,warheads, deterrent,face,continued,advances]
这是我尝试过的代码
for text in df['Filtered_data'].iteritems():
for i in text.split():
print (i)
推荐答案
结合使用列表理解和 split
并扁平化:
Use list comprehension with split
and flatenning:
df['Filtered_data'] = df['Filtered_data'].apply(lambda x: [z for y in x for z in y.split()])
print (df)
Filtered_data
0 [defence, possessed, russia, china, factors, d...
1 [force, bolster, pentagon, strike, capabilitie...
2 [missiles, warheads, deterrent, face, continue...
对于唯一值是标准方式,请使用 set
s:
For unique values is standard way use set
s:
df['Filtered_data'] = df['Filtered_data'].apply(lambda x: list(set([z for y in x for z in y.split()])))
print (df)
Filtered_data
0 [russia, factors, defence, driving, china, mod...
1 [capabilities, detailing, china, force, pentag...
2 [deterrent, advances, face, warheads, missiles...
但是,如果值的排序很重要,请使用 pandas.unique
:
But if ordering of values is important use pandas.unique
:
df['Filtered_data'] = df['Filtered_data'].apply(lambda x: pd.unique([z for y in x for z in y.split()]).tolist())
print (df)
Filtered_data
0 [defence, possessed, russia, china, factors, d...
1 [force, bolster, pentagon, strike, capabilitie...
2 [missiles, warheads, deterrent, face, continue...
这篇关于在 pandas 数据框中将列表元素拆分为子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!