Ste*_*e T 1 python split unique pandas
我开始使用pandas,并在更大的DataFrame中有一列数据,例如
0 one two
1 two seven six
2 three one five
3 seven five five eight
4 six four
5 three
dtype: object
Run Code Online (Sandbox Code Playgroud)
我想要做的是将单词序列分成它们的组成部分,然后得到一个独特的集合或计算单词.我可以做好分裂
numbers.str.split(' ')
0 [one, two]
1 [two, seven, six]
2 [three, one, five]
3 [seven, five, five, eight]
4 [six, four]
5 [three]
dtype: object
Run Code Online (Sandbox Code Playgroud)
但是,我不知道从哪里开始.再一次,我想要输出如
['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight']
Run Code Online (Sandbox Code Playgroud)
或者在具有计数的字典中,或者在与这两者中的一个相当的Series/DataFrame中.
到目前为止我能做的最好的事情是将apply()与Set结合使用来获得独特的单词.从我到目前为止看到的熊猫是一个非常优雅的包装,似乎这对于比我更了解它的人来说可能很容易实现.
提前致谢!
如果我理解正确,我认为你可以使用熊猫这样做.在分割字符串之前,我将从系列开始:
print s
0 one two
1 two seven six
2 three one five
3 seven five five eight
4 six four
5 three
stacked = pd.DataFrame(s.str.split().tolist()).stack()
print stacked
0 0 one
1 two
1 0 two
1 seven
2 six
2 0 three
1 one
2 five
3 0 seven
1 five
2 five
3 eight
4 0 six
1 four
5 0 three
Run Code Online (Sandbox Code Playgroud)
现在只需计算系列的值计数:
print stacked.value_counts()
five 3
one 2
three 2
six 2
two 2
seven 2
eight 1
four 1
dtype: int64
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2174 次 |
| 最近记录: |