[英]Python: In a dataframe, create a new column with a string sliced from a column with the value of another column
I have a pretty simple problem that i can't get around. 我有一个很简单的问题,我无法解决。
I create a dataframe and i would like to generate a new column with a sliced string from one column with a slice value from another column. 我创建一个数据框,我想生成一个新列,其中一个列的切片字符串与另一列的切片值生成。
Ex: From this: 例如:从此:
dftest = pd.DataFrame({'string' : ['EXAMPLE']*5, 'position' : [1, 2, 3, 4, 5]})
position string
0 1 EXAMPLE
1 2 EXAMPLE
2 3 EXAMPLE
3 4 EXAMPLE
4 5 EXAMPLE
5 1 OTHER
6 2 OTHER
7 3 OTHER
I want this: 我要这个:
position string new
0 1 EXAMPLE E
1 2 EXAMPLE EX
2 3 EXAMPLE EXA
3 4 EXAMPLE EXAM
4 5 EXAMPLE EXAMP
5 1 OTHER O
6 2 OTHER OT
7 3 OTHER OTH
I tried: 我试过了:
dftest['new'] = dftest.string.str[:dftest.position]
dftest['new'] = dftest.string.str[:dftest['position']]
dftest['new'] = dftest.string[:dftest.position]
as well as different row iteration methods but each time I end up with Nan values. 以及不同的行迭代方法,但每次我最终得到Nan值时。
Any help would be greatly appreciated 任何帮助将不胜感激
You can do the following 您可以执行以下操作
dftest['new'] = [dftest.iloc[i]['string'][0:dftest.iloc[i]['position']] for i in range(0,len(dftest))]
This will check the position. 这将检查位置。
One method is to enumerate the string using a list comprehension. 一种方法是使用列表推导枚举字符串。
dftest['new'] = [s[:n] for s, n in zip(dftest.string, dftest.position)]
>>> dftest
position string new
0 1 EXAMPLE E
1 2 EXAMPLE EX
2 3 EXAMPLE EXA
3 4 EXAMPLE EXAM
4 5 EXAMPLE EXAMP
5 1 OTHER O
6 2 OTHER OT
7 3 OTHER OTH
You could use iterrows
method: 您可以使用iterrows
方法:
for i, row in df.iterrows():
df.loc[i, 'new'] = row['string'][:row['position']]
Example: 例:
In [60]: dftest
Out[60]:
position string
0 1 EXAMPLE
1 2 EXAMPLE
2 3 EXAMPLE
3 4 EXAMPLE
4 5 EXAMPLE
5 1 OTHER
6 2 OTHER
7 3 OTHER
for i, row in dftest.iterrows():
dftest.loc[i, 'new'] = row['string'][:row['position']]
In [62]: dftest
Out[62]:
position string new
0 1 EXAMPLE E
1 2 EXAMPLE EX
2 3 EXAMPLE EXA
3 4 EXAMPLE EXAM
4 5 EXAMPLE EXAMP
5 1 OTHER O
6 2 OTHER OT
7 3 OTHER OTH
EDIT 编辑
Or you could use apply
which is more convenient: 或者你可以使用apply
这样更方便:
dftest.apply(lambda x: x['string'][:x['position']], axis=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.