為什么這會導致 NaN 而不是字串?
df['rnd'] = df.apply(lambda x: str(random.randint(0,9999)).zfill(4))
rnd
---
NaN
NaN
NaN
NaN
NaN
df.dtypes
Rnd object
``
uj5u.com熱心網友回復:
你需要 applymap 不適用
df['rnd'] = df.applymap(lambda x: str(random.randint(0,9999)).zfill(4))
這就是結果
0 0179
1 4545
2 8510
3 3316
Name: rnd, dtype: object
如果你想使用 apply 方法,那么你不需要指定要操作的列:
df["rnd"] = df.rnd.apply(lambda x: str(random.randint(0,9999)).zfill(4))
uj5u.com熱心網友回復:
它需要一個axis.
df['rnd'] = df.applymap(lambda x: str(random.randint(0,9999)).zfill(4), axis=1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/331635.html
上一篇:類中的Python條件陳述句
