[英]Python: In a dataframe, create a new column with a string sliced from a column with the value of another column
我有一個很簡單的問題,我無法解決。
我創建一個數據框,我想生成一個新列,其中一個列的切片字符串與另一列的切片值生成。
例如:從此:
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
我要這個:
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
我試過了:
dftest['new'] = dftest.string.str[:dftest.position]
dftest['new'] = dftest.string.str[:dftest['position']]
dftest['new'] = dftest.string[:dftest.position]
以及不同的行迭代方法,但每次我最終得到Nan值時。
任何幫助將不勝感激
您可以執行以下操作
dftest['new'] = [dftest.iloc[i]['string'][0:dftest.iloc[i]['position']] for i in range(0,len(dftest))]
這將檢查位置。
一種方法是使用列表推導枚舉字符串。
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
您可以使用iterrows
方法:
for i, row in df.iterrows():
df.loc[i, 'new'] = row['string'][:row['position']]
例:
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
編輯
或者你可以使用apply
這樣更方便:
dftest.apply(lambda x: x['string'][:x['position']], axis=1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.