Home > Blockchain >  How to iterate over columns using pandas?
How to iterate over columns using pandas?

Time:10-24

I want to iterate over columns? However, I get a warning on iterating and the values I get are also incorrect. Please, guide in this regard.

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

Warning

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)

CodePudding user response:

Don't iterate over columns. You can use the vectorized version below:

>>> 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

Details:

>>> 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

CodePudding user response:

Use the getitem Syntax to Iterate Over Columns in 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() to Iterate Over Columns in 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() to Iterate Over Columns 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)
  • Related