![](/img/trans.png)
[英]apply() to every column of every dataframe of an ExcelFile, Pandas
[英]Pandas apply & map to every element of every column
如果其值不為null,如何將自定義函數應用於每個列的每個元素?
可以說我有一個10列的數據框,如果pd.notnull(x),我想在其中應用一個lower()函數到只有4列的每個元素上,否則就不保留任何值。
我試圖這樣使用
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
但是由於我的列是混合數據類型(浮點數為NaN,其余為unicode)。 這給我拋出了一個錯誤-
float has no attribute map.
如何擺脫這個錯誤?
我認為您需要DataFrame.applymap
因為按DataFrame.applymap
工作:
L = [[1.5, 'Test', np.nan, 2], ['Test', np.nan, 2,'TEST'], ['Test', np.nan,1.5, 2]]
df = pd.DataFrame(L, columns=list('abcd'))
print (df)
a b c d
0 1.5 Test NaN 2
1 Test NaN 2.0 TEST
2 Test NaN 1.5 2
cols = ['a','b']
#for python 2 change str to basestring
df[cols] = df[cols].applymap(lambda x: x.lower() if isinstance(x, str) else x)
print (df)
a b c d
0 1.5 test NaN 2
1 test NaN 2.0 TEST
2 test NaN 1.5 2
您嘗試映射一個Series,然后在lambda中占據整行。
您還應該檢查沒有方法.lower()的整數,浮點數等。 因此,我認為最好的方法是檢查它是否是字符串,而不僅僅是檢查它是否不是非空值。
這有效:
s = pd.DataFrame([{'A': 1.5, 'B':"Test", 'C': np.nan, 'D':2}])
s
A B C D
0 1.5 Test NaN 2
s1 = s.apply(lambda x: x[0].lower() if isinstance(x[0], basestring) else x[0]).copy()
s1
A 1.5
B test
C NaN
D 2
dtype: object
對於python 3,檢查字符串isinstance(x[0], str)
為了能夠選擇列:
s1 = pd.DataFrame()
columns = ["A", "B"]
for column in columns:
s1[column] = s[column].apply(lambda x: x.lower() if isinstance(x, str) else x).copy()
s1
A B
0 1.5 test
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.