6

私はPDシリーズを持っています。

s = pd.Series([1, 2, 3, np.nan])

私がする時、

s.map('this is a string {}'.format)
[out]
0    this is a string 1.0
1    this is a string 2.0
2    this is a string 3.0
3    this is a string nan   

フォーマットされた文字列を使用して同じ結果を得るにはどうすればよいですか?

s.map(f'this is a string {?}') ?
4

2 に答える 2

2

map/ apply+なしで解決できlambdaます。リストをシリーズに直接割り当てることができます。pd.Series.applyリスト内包表記はベクトル化されていないため、多くの場合より効率的です。

df = pd.DataFrame({'s': pd.Series([1, 2, 3, np.nan])})

df['t'] = [f'this is a string {i}' for i in df['s']]

print(df)

     s                     t
0  1.0  this is a string 1.0
1  2.0  this is a string 2.0
2  3.0  this is a string 3.0
3  NaN  this is a string nan
于 2018-09-29T15:50:05.107 に答える