简体   繁体   English

根据pandas数据框中的另一列获取子字符串

[英]Getting substring based on another column in a pandas dataframe

Hi is there a way to get a substring of a column based on another column? 您有没有办法根据另一列获取列的子字符串?

import pandas as pd
x = pd.DataFrame({'name':['bernard','brenden','bern'],'digit':[2,3,3]})
x

     digit  name
0   2   bernard
1   3   brenden
2   3   bern

What i would expect is something like: 我期望的是:

for row in x.itertuples():
    print row[2][:row[1]]

be
bre
ber

where the result is the substring of name based on digit. 其中结果是基于数字的名称子字符串。

I know if I really want to I can create a list based on the itertuples function but does not seem right and also, I always try to create a vectorized method. 我知道如果我真的想要我可以创建一个基于itertuples函数的列表,但似乎不对,而且,我总是尝试创建一个矢量化方法。

Appreciate any feedback. 感谢任何反馈。

Use apply with axis=1 for row-wise with a lambda so you access each column for slicing: 使用apply axis=1 for row-wise with lambda以便访问每列进行切片:

In [68]:
x = pd.DataFrame({'name':['bernard','brenden','bern'],'digit':[2,3,3]})
x.apply(lambda x: x['name'][:x['digit']], axis=1)

Out[68]:
0     be
1    bre
2    ber
dtype: object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM