我想遍歷列?但是,我在迭代時收到警告,并且我得到的值也不正確。請在這方面進行指導。
for column in X3:
mean = np.mean(X3[column])
max = np.max(X3[column])
min = np.min(X3[column])
X3[column] = (X3[column] - mean)/(max - min)
X3
警告
C:\Users\DELL55~1\AppData\Local\Temp/ipykernel_12136/2368760487.py:9: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
X3[column] = (X3[column] - mean)/(max - min)
uj5u.com熱心網友回復:
不要迭代列。您可以使用下面的矢量化版本:
>>> X3
A B C D
0 8 7 12 4
1 15 10 7 5
2 13 16 18 7
3 2 5 13 15
4 17 11 17 6
>>> (X3 - X3.mean()) / (X3.max() - X3.min())
A B C D
0 -0.200000 -0.254545 -0.127273 -0.309091
1 0.266667 0.018182 -0.581818 -0.218182
2 0.133333 0.563636 0.418182 -0.036364
3 -0.600000 -0.436364 -0.036364 0.690909
4 0.400000 0.109091 0.327273 -0.127273
細節:
>>> X3.mean()
A 11.0
B 9.8
C 13.4
D 7.4
dtype: float64
>>> X3.max()
A 17
B 16
C 18
D 15
dtype: int64
>>> X3.min()
A 2
B 5
C 7
D 4
dtype: int64
uj5u.com熱心網友回復:
使用 getitem 語法迭代 Pandas DataFrame 中的列。
import pandas as pd
df = pd.DataFrame([[10,6,7,8],
[1,9,12,14],
[5,8,10,6]],
columns = ['a','b','c','d'])
print(df)
print("------------------")
for column in df:
print(df[column].values)
dataframe.iteritems() 迭代 Pandas Dataframe 中的列。
import pandas as pd
df = pd.DataFrame([[10,6,7,8],
[1,9,12,14],
[5,8,10,6]],
columns = ['a','b','c','d'])
for (colname,colval) in df.iteritems():
print(colname, colval.values)
enumerate() 迭代列 Pandas。
import pandas as pd
df = pd.DataFrame([[10,6,7,8],
[1,9,12,14],
[5,8,10,6]],
columns = ['a','b','c','d'])
for (index, colname) in enumerate(df):
print(index, df[colname].values)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/333431.html
上一篇:用相同條件替換多個列的現有列
