主要用於一個大型的 DataFrame 怎麼把其中某個 Column 的值減去某個特定值,雖然直觀但是超好用的! 一定要記下來XD import pandas as pd ds = pd.DataFrame(np.arange(12).reshape(3,4), cols=['A','B','C','Average']) ds['A'] = ds['A'] - ds['Average'] ds['B'] = ds['B'] - ds['Average'] ds['C'] = ds['C'] - ds['Average'] #Pandas is easy like that! #Oh, that does it for the entire DF. You only want it for the firs row is that right? ds.loc[1, 'A'] = ds.loc[1, 'A'] - ds.loc[1, 'Average'] ds.loc[1, 'B'] = ds.loc[1, 'B'] - ds.loc[1, 'Average'] ds.loc[1, 'C'] = ds.loc[1, 'C'] - ds.loc[1, 'Average'] #or in a loop: for col in ['A', 'B', 'C']: ds.loc[1, col] = df.loc[1, col] - ds.loc[1, 'Average'] #and so on... #if you have thousands of columns then simply do: for col in ds.columns: ds[col] = ds[col] - ds[
不定時更新學習的新事物與大家分享! (主力在 Python, 遊戲化與教育。)