python-对熊猫DataFram中的列进行.str.split()操作后获取最后一个“列”
我在pandas DataFrame中有一列想要在一个空格上拆分。 使用temp2[0]进行拆分非常简单,但是我无法从最后一个条目中创建新列。 当我temp2[:][-1]的列时,我会得到一个数组列表,而且我不知道如何操纵该列来为DataFrame获取新的列。
这是一个例子。 列中的每个条目都包含“符号数据价格”,我想将价格分开(最终在一半的情况下删除“ p” ...或“ c”)。
import pandas as pd
temp = pd.DataFrame({'ticker' : ['spx 5/25/2001 p500', 'spx 5/25/2001 p600', 'spx 5/25/2001 p700']})
temp2 = temp.ticker.str.split(' ')
产生
0 ['spx', '5/25/2001', 'p500']
1 ['spx', '5/25/2001', 'p600']
2 ['spx', '5/25/2001', 'p700']
但是temp2[0]仅给出一个列表条目的数组,而temp2[:][-1]失败。 如何将每个数组中的最后一个条目转换为新列? 谢谢!
5个解决方案
100 votes
做这个:
In [43]: temp2.str[-1]
Out[43]:
0 p500
1 p600
2 p700
Name: ticker
Wes McKinney answered 2020-02-15T07:18:45Z
36 votes
您可以使用apply方法作为中介:
In [99]: import pandas as pd
In [100]: d1 = pd.DataFrame({'ticker' : ['spx 5/25/2001 p500', 'spx 5/25/2001 p600', 'spx 5/25/2001 p700']})
In [101]: d1.ticker.str.split().tolist()
Out[101]:
[['spx', '5/25/2001', 'p500'],
['spx', '5/25/2001', 'p600'],
['spx', '5/25/2001', 'p700']]
从中可以制作新的DataFrame:
In [102]: d2 = pd.DataFrame(d1.ticker.str.split().tolist(),
.....: columns="symbol date price".split())
In [103]: d2
Out[103]:
symbol date price
0 spx 5/25/2001 p500
1 spx 5/25/2001 p600
2 spx 5/25/2001 p700
从好的方面来说,您可以确定价格:
In [104]: d2["price"] = d2["price"].str.replace("p","").astype(float)
In [105]: d2
Out[105]:
symbol date price
0 spx 5/25/2001 500
1 spx 5/25/2001 600
2 spx 5/25/2001 700
PS:但是,如果您真的只想最后一列,apply就足够了:
In [113]: temp2.apply(lambda x: x[2])
Out[113]:
0 p500
1 p600
2 p700
Name: ticker
DSM answered 2020-02-15T07:18:26Z
17 votes
[https://pandas.pydata.org/pandas-docs/stable/text.html]
s2 = pd.Series(['a_b_c', 'c_d_e', np.nan, 'f_g_h'])
s2.str.split('_').str.get(1)
要么
s2.str.split('_').str[1]
James Holland answered 2020-02-15T07:19:10Z
3 votes
使用熊猫0.20.3:
In [10]: import pandas as pd
...: temp = pd.DataFrame({'ticker' : ['spx 5/25/2001 p500', 'spx 5/25/2001 p600', 'spx 5/25/2001 p700']})
...:
In [11]: temp2 = temp.ticker.str.split(' ', expand=True) # the expand=True return a DataFrame
In [12]: temp2
Out[12]:
0 1 2
0 spx 5/25/2001 p500
1 spx 5/25/2001 p600
2 spx 5/25/2001 p700
In [13]: temp3 = temp.join(temp2[2])
In [14]: temp3
Out[14]:
ticker 2
0 spx 5/25/2001 p500 p500
1 spx 5/25/2001 p600 p600
2 spx 5/25/2001 p700 p700
AllanLRH answered 2020-02-15T07:19:30Z
0 votes
如果您正在寻找单线(就像我来这里一样),这应该做得很好:
temp2 = temp.ticker.str.split(' ', expand = True)[-1]
您还可以按如下方式简单地修改此答案,以将此列分配回原始DataFrame:
temp['last_split'] = temp.ticker.str.split(' ', expand = True)[-1]
我想这是一个流行的用例。
sfortney answered 2020-02-15T07:19:59Z