对于这个数据文件:
import pandas as pd
df=pd.DataFrame([[2],['do-g'],['ra-t'],['ca-t'],[5]], columns=['A'])
print(df)对于列中的值,'A‘被视为'int’值。如何添加一个新列(从'A‘列派生),从单词中删除'-’,但保留数字/整数?
我尝试的解决方案:
df['new_column']=df.A.apply(lambda x: x.replace('-') if x.isnull() else x)发布于 2020-11-15 09:22:27
您可以忽略lambda中的非字符串值。
>>> df['new_column'] = df.A.apply(lambda x: x.replace('-', '') if isinstance(x,str) else x)
>>> df
A new_column
0 2 2
1 do-g dog
2 ra-t rat
3 ca-t cat
4 5 5https://stackoverflow.com/questions/64842798
复制相似问题