使用Pandas将多行的零值更改为众数。

huangapple go评论88阅读模式
英文:

Change the zero values of multiple rows with mode in Pandas

问题

我想更改8列中的'0'值,使用众数方法。0不仅是这些列的值,还有其他值。我只想更改这些列中的0值为众数值。假设数据集如下:

col1 col2 col3 col4 col5 col6 col7 col8 col9 col10

我想一次性从col3到col10更改'0'值。

我使用以下代码将0更改为众数:

replace_col = df['col3']
replace_col.replace(to_replace=0, value=replace_col.mode()[0], inplace=True)

但是,我需要更改df的值,例如df['col3'],df['col4'],df['col4']每次。所以我需要运行8次代码来更改8列的值。是否有一种方法可以一次运行一个代码片段来更改一个值?

谢谢。

英文:

I have a dataset with 10 columns. Among them I want to change the '0' values of 8 columns with mode methods. 0 is not only the values of those columns, there are other values too. Among them I only want change the 0 values with mode. Suppose the dataset looks like this,

  1. col1 col2 col3 col4 col5 col6 col7 col8 col9 col10

I want to change the '0' values from col3 to col10 at a time.

I used the following code to change 0 to mode

  1. replace_col = df['col3']
  2. replace_col.replace(to_replace = 0, value = replace_col.mode()[0], inplace=True)

However, I need to change the values of df, such as df['col3'], df['col4'], df['col4'] each time. So I need to run the code 8 times to change the values of 8 columns. Is there any way I can change the value at a time running one code snippet?

Thank you.

答案1

得分: 1

  1. import pandas as pd
  2. # 示例数据框
  3. data = {
  4. 'col1': [1, 2, 3, 4, 5],
  5. 'col2': [0, 6, 7, 0, 9],
  6. 'col3': [10, 0, 0, 13, 14],
  7. 'col4': [15, 0, 17, 18, 19],
  8. 'col5': [0, 21, 22, 23, 0],
  9. 'col6': [25, 0, 0, 28, 0],
  10. 'col7': [30, 31, 0, 33, 0],
  11. 'col8': [0, 35, 36, 0, 0],
  12. 'col9': [40, 41, 42, 43, 44],
  13. 'col10': [0, 51, 52, 53, 0]
  14. }
  15. df = pd.DataFrame(data)
  16. # 要更新的列
  17. cols_to_update = ['col3', 'col4', 'col5', 'col6', 'col7', 'col8', 'col9', 'col10']
  18. # 将每列中的0值替换为众数
  19. for col in cols_to_update:
  20. replace_col = df[col]
  21. mode_value = replace_col.mode()[0]
  22. replace_col.replace(to_replace=0, value=mode_value, inplace=True)
  23. print(df)
英文:
  1. import pandas as pd
  2. # Sample DataFrame
  3. data = {
  4. 'col1': [1, 2, 3, 4, 5],
  5. 'col2': [0, 6, 7, 0, 9],
  6. 'col3': [10, 0, 0, 13, 14],
  7. 'col4': [15, 0, 17, 18, 19],
  8. 'col5': [0, 21, 22, 23, 0],
  9. 'col6': [25, 0, 0, 28, 0],
  10. 'col7': [30, 31, 0, 33, 0],
  11. 'col8': [0, 35, 36, 0, 0],
  12. 'col9': [40, 41, 42, 43, 44],
  13. 'col10': [0, 51, 52, 53, 0]
  14. }
  15. df = pd.DataFrame(data)
  16. # Columns to update
  17. cols_to_update = ['col3', 'col4', 'col5', 'col6', 'col7', 'col8', 'col9', 'col10']
  18. # Replace 0 values with mode for each column
  19. for col in cols_to_update:
  20. replace_col = df[col]
  21. mode_value = replace_col.mode()[0]
  22. replace_col.replace(to_replace=0, value=mode_value, inplace=True)
  23. print(df)

答案2

得分: 1

  1. 在你的情况下执行:
  2. df = df.mask(df==0, df.mask(df==0).mode().loc[0], axis=1)
英文:

In your case do

  1. df = df.mask(df==0,df.mask(df==0).mode().loc[0],axis=1)

答案3

得分: 1

这是我的答案:

  1. import pandas as pd
  2. # 示例数据框
  3. data = {
  4. 'col1': [1, 2, 3, 4, 5],
  5. 'col2': [0, 6, 7, 0, 9],
  6. 'col3': [10, 0, 0, 13, 14],
  7. 'col4': [15, 0, 17, 18, 19],
  8. 'col5': [0, 21, 22, 23, 0],
  9. 'col6': [25, 0, 0, 28, 0],
  10. 'col7': [30, 31, 0, 33, 0],
  11. 'col8': [0, 35, 36, 0, 0],
  12. 'col9': [40, 41, 42, 43, 44],
  13. 'col10': [0, 51, 52, 53, 0]
  14. }
  15. df = pd.DataFrame(data)
  16. print(df)
  17. df1 = df.copy()
  18. cols = df.columns
  19. # 将0替换为999999
  20. df1[cols] = df1[cols].replace(0, 999999)
  21. print(df1)

输出结果如下:

  1. col1 col2 col3 col4 col5 col6 col7 col8 col9 col10
  2. 0 1 0 10 15 0 25 30 0 40 0
  3. 1 2 6 0 0 21 0 31 35 41 51
  4. 2 3 7 0 17 22 0 0 36 42 52
  5. 3 4 0 13 18 23 28 33 0 43 53
  6. 4 5 9 14 19 0 0 0 0 44 0
  7. col1 col2 col3 col4 col5 col6 col7 col8 col9 col10
  8. 0 1 999999 10 15 999999 25 30 999999 40 999999
  9. 1 2 6 999999 999999 21 999999 31 35 41 51
  10. 2 3 7 999999 17 22 999999 999999 36 42 52
  11. 3 4 999999 13 18 23 28 33 999999 43 53
  12. 4 5 9 14 19 999999 999999 999999 999999 44 999999
英文:

Here my answer:

  1. import pandas as pd
  2. # Sample DataFrame
  3. data = {
  4. 'col1': [1, 2, 3, 4, 5],
  5. 'col2': [0, 6, 7, 0, 9],
  6. 'col3': [10, 0, 0, 13, 14],
  7. 'col4': [15, 0, 17, 18, 19],
  8. 'col5': [0, 21, 22, 23, 0],
  9. 'col6': [25, 0, 0, 28, 0],
  10. 'col7': [30, 31, 0, 33, 0],
  11. 'col8': [0, 35, 36, 0, 0],
  12. 'col9': [40, 41, 42, 43, 44],
  13. 'col10': [0, 51, 52, 53, 0]
  14. }
  15. df = pd.DataFrame(data)
  16. print(df)
  17. df1 = df.copy()
  18. cols = df.columns
  19. # inplace of 999999 you can put anything
  20. df1[cols] = df1[cols].replace(0, 999999)
  21. print(df1)
  22. col1 col2 col3 col4 col5 col6 col7 col8 col9 col10
  23. 0 1 999999 10 15 999999 25 30 999999 40 999999
  24. 1 2 6 999999 999999 21 999999 31 35 41 51
  25. 2 3 7 999999 17 22 999999 999999 36 42 52
  26. 3 4 999999 13 18 23 28 33 999999 43 53
  27. 4 5 9 14 19 999999 999999 999999 999999 44 999999

huangapple
  • 本文由 发表于 2023年7月18日 01:55:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定