首先是必要的split
值,将其转换为整数,将add 1
,转换为字符串并通过;
以下方式加入:
df['Values'] = df['Values'].apply(lambda x: ';'.join(str(int(y) + 1) for y in x.split(';')))
列表理解的解决方案:
df['Values'] = [ ';'.join(str(int(y) + 1) for y in x.split(';')) for x in df['Values']]
print (df)
Name ID Values
0 John 81-502 2
1 Mike 81-501 3;3;3
2 Matthew 81-512 2;1
快速发行!如果df ['Values'] [0] == NaN怎么办
@ user171558-然后使用
.apply(lambda x: ';'.join(str(int(y) + 1) for y in x.split(';')) if pd.notna(x) else np.nan)
在我实际的df中,我有一些字符串,这些字符串会输入到package函数中。我的值列由字符串组成。如果我使用pd.NA而不是np.nan可以吗?经过一番阅读,我认为它们非常相似
@ user171558-我认为是一样的:)