1 回答

TA貢獻1824條經驗 獲得超8個贊
您只能使用表達式。請參閱 python文檔。
只需將它括在括號中,python 解釋器就會很高興。
lambda x: (x.split('\\')[0]) & (pd.to_datetime(x, yearfirst=True) > pd.to_datetime('2004-06-23',yearfirst=True)))
但是,您正在將函數(shù)應用于相應的表“LastWriteTime”和“FullName”,函數(shù)更適合。創(chuàng)建多個評估函數(shù)并將其與相應的鍵存儲在字典中,并傳遞給 aggregate()。
def check_date(x):
return pd.to_datetime(x) > pd.to_datetime('2004-06-23', yearfirst=True)
def files_count(x):
return x.split('\\')[0]
df1 = pd.DataFrame(data)
func_dict = {'FullName': files_count, 'LastWriteTime': check_date}
df4 = df1.aggregate(func_dict) # applying multiple function to multiple columns
print(df4, end='\n'*2)
# use df[df['a']] & df[df['b']] for filtering.
df3_filtered = df4[df4['LastWriteTime']] # filtering.
print(df3_filtered, end='\n'*2)
df2 = df3_filtered['FullName'].value_counts() # counting.
print(df2)
結果:
FullName LastWriteTime
0 dog False
1 feline False
2 dog True
3 mouse True
4 anteater True
5 dog True
FullName LastWriteTime
2 dog True
3 mouse True
4 anteater True
5 dog True
dog 2
anteater 1
mouse 1
Name: FullName, dtype: int64
添加回答
舉報