简体   繁体   English

Python:在数据帧中,创建一个新列,并使用从另一列的值中切出的字符串

[英]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.

相关问题 创建新的数据框列,保留另一列的第一个值 - Create new dataframe column keeping the first value from another column 如果该列包含另一个数据框的列中的字符串,则在该数据框中创建一个新列 - Create a new column in a dataframe if the column contains a string from a column of another dataframe 如何使用python中另一列的值创建一个新列 - How to create a new column with a value from another column in python 从Python中的另一列创建新列 - Create a new column from another column in Python Python:在DataFrame中,在新列中为另一列中具有最高值的行添加值,在第三列中添加相同的字符串 - Python: In DataFrame, add value in a new column for row with highest value in another column and string identical in a third one Pandas数据框的切片列在从该列创建的新对象中不断提及原始列名 - Sliced column of Pandas dataframe keep mentioning original column name in new objects created from the column 通过解析列值为数据框创建新列,并使用来自另一列python的值填充新列 - Create new columns for a dataframe by parsing column values and populate new columns with values from another column python 如何创建一个新的数据框列,并从另一个列中移出值? - How to create a new dataframe column with shifted values from another column? 如果来自另一个 dataframe 的列和来自原始 dataframe 的列具有匹配值,则在原始 dataframe 中创建一个新列 - Create a new column in the original dataframe if the column from another dataframe and a column from original dataframe have matching values Python Dataframe 根据另一列的值向新列添加值 - Python Dataframe add a value to new column based on value from another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM