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

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

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,

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

replace_col = df['col3']
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

import pandas as pd

# 示例数据框
data = {
    'col1': [1, 2, 3, 4, 5],
    'col2': [0, 6, 7, 0, 9],
    'col3': [10, 0, 0, 13, 14],
    'col4': [15, 0, 17, 18, 19],
    'col5': [0, 21, 22, 23, 0],
    'col6': [25, 0, 0, 28, 0],
    'col7': [30, 31, 0, 33, 0],
    'col8': [0, 35, 36, 0, 0],
    'col9': [40, 41, 42, 43, 44],
    'col10': [0, 51, 52, 53, 0]
}

df = pd.DataFrame(data)

# 要更新的列
cols_to_update = ['col3', 'col4', 'col5', 'col6', 'col7', 'col8', 'col9', 'col10']

# 将每列中的0值替换为众数
for col in cols_to_update:
    replace_col = df[col]
    mode_value = replace_col.mode()[0]
    replace_col.replace(to_replace=0, value=mode_value, inplace=True)

print(df)
英文:
import pandas as pd

# Sample DataFrame
data = {
    'col1': [1, 2, 3, 4, 5],
    'col2': [0, 6, 7, 0, 9],
    'col3': [10, 0, 0, 13, 14],
    'col4': [15, 0, 17, 18, 19],
    'col5': [0, 21, 22, 23, 0],
    'col6': [25, 0, 0, 28, 0],
    'col7': [30, 31, 0, 33, 0],
    'col8': [0, 35, 36, 0, 0],
    'col9': [40, 41, 42, 43, 44],
    'col10': [0, 51, 52, 53, 0]
}

df = pd.DataFrame(data)

# Columns to update
cols_to_update = ['col3', 'col4', 'col5', 'col6', 'col7', 'col8', 'col9', 'col10']

# Replace 0 values with mode for each column
for col in cols_to_update:
    replace_col = df[col]
    mode_value = replace_col.mode()[0]
    replace_col.replace(to_replace=0, value=mode_value, inplace=True)

print(df)

答案2

得分: 1

在你的情况下执行:

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

In your case do

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

答案3

得分: 1

这是我的答案:

import pandas as pd

# 示例数据框
data = {
    'col1': [1, 2, 3, 4, 5],
    'col2': [0, 6, 7, 0, 9],
    'col3': [10, 0, 0, 13, 14],
    'col4': [15, 0, 17, 18, 19],
    'col5': [0, 21, 22, 23, 0],
    'col6': [25, 0, 0, 28, 0],
    'col7': [30, 31, 0, 33, 0],
    'col8': [0, 35, 36, 0, 0],
    'col9': [40, 41, 42, 43, 44],
    'col10': [0, 51, 52, 53, 0]
}
df = pd.DataFrame(data)

print(df)
df1 = df.copy()
cols = df.columns
# 将0替换为999999
df1[cols] = df1[cols].replace(0, 999999)
print(df1)

输出结果如下:

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

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

Here my answer:

import pandas as pd

# Sample DataFrame
data = {
    'col1': [1, 2, 3, 4, 5],
    'col2': [0, 6, 7, 0, 9],
    'col3': [10, 0, 0, 13, 14],
    'col4': [15, 0, 17, 18, 19],
    'col5': [0, 21, 22, 23, 0],
    'col6': [25, 0, 0, 28, 0],
    'col7': [30, 31, 0, 33, 0],
    'col8': [0, 35, 36, 0, 0],
    'col9': [40, 41, 42, 43, 44],
    'col10': [0, 51, 52, 53, 0]
}
df = pd.DataFrame(data)

print(df)
df1 = df.copy()
cols = df.columns
# inplace of 999999 you can put anything
df1[cols] = df1[cols].replace(0, 999999)
print(df1)

 col1    col2    col3    col4    col5    col6    col7    col8  col9   col10
0     1  999999      10      15  999999      25      30  999999    40  999999
1     2       6  999999  999999      21  999999      31      35    41      51
2     3       7  999999      17      22  999999  999999      36    42      52
3     4  999999      13      18      23      28      33  999999    43      53
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:

确定