【发布时间】:2015-02-05 09:17:20
【问题描述】:
我正在寻找 pandas 中的子字符串功能:给定一系列位置,我想从每一行 i 中选择子字符串 [0:pos_i]:
>>> text = pd.Series(['123456789', '987654321'])
0 123456789
1 987654321
dtype: object
>>> pos = pd.Series([3,6])
0 3
1 6
dtype: int64
输出应该是:
>>> pd.Series(['123', '987654'])
0 123
1 987654
dtype: object
在一个数据框中拆分成两列会更好:
>>> pd.DataFrame([['123', '456789'], ['987654', '321']])
0 1
0 123 456789
1 987654 321
【问题讨论】:
-
我的回答解决了你的问题吗?
标签: python string pandas substring