値がnullでない場合、各列のすべての要素にカスタム関数を適用する方法はありますか?パンダはすべての列のすべての要素に適用してマップします
私は、pd.notnull(x)の場合は4列のすべての要素にlower()関数を適用し、それ以外の場合は値として保持しないように10列のデータフレームがあるとします。
私は
s.apply(lambda x: change_to_lowercase(x), axis = 1)
def change_to_lowercase(s):
s['A'] = s['A'].map(lambda x: x.lower() if pd.notnull(x) else x)
s['B'] = s['B'].map(lambda x: x.lower() if pd.notnull(x) else x)
s['C'] = s['C'].map(lambda x: x.lower() if pd.notnull(x) else x)
s['D'] = s['D'].map(lambda x: x.lower() if pd.notnull(x) else x)
return s
、このように使用してみました。しかし、私の列は、(Unicodeとしてフロート、残りの部分としてNaNである)混合データ型であるため。これは私にエラーを投げています -
float has no attribute map.
このエラーを取り除く方法はありますか?
ありがとうございます。それは理にかなっている。これをデータフレームの一部の列に対してのみ適用するにはどうすればよいですか? –
@ds_user updated answer –