Pandas Dataframe - 添加其他内容?

发布于 2025-01-18 05:04:39 字数 1311 浏览 3 评论 0原文

我想为我的贝叶斯网络生成测试数据。 这是我当前的代码:

data = np.random.randint(2, size=(5, 6))
columns = ['p_1', 'p_2', 'OP1', 'OP2', 'OP3', 'OP4']
df = pd.DataFrame(data=data, columns=columns)

df.loc[(df['p_1'] == 1) & (df['p_2'] == 1), 'OP1'] = 1

df.loc[(df['p_1'] == 1) & (df['p_2'] == 0), 'OP2'] = 1

df.loc[(df['p_1'] == 0) & (df['p_2'] == 1), 'OP3'] = 1

df.loc[(df['p_1'] == 0) & (df['p_2'] == 0), 'OP4'] = 1


print(df)

因此,例如,每次P_1都有1和P_2具有1,OP1也应为1,所有其他值都应在列中输出0。 当P_1为1,P_2为0时,OP2应为1和其他所有0,依此类推。

但是我当前的输出如下:

P_1P_1 P_2OP1OP2 OP3OP3OP4
0000 0 001
110 1 111 11 1
1 01 01101 0
11 1 11111 1
100110

是否有任何方法可以修复它?我做错了什么?

我并不真正了解其他人问题的解决方案,所以我认为ID在这里问。

我希望有人可以帮助我。

I want to generate Test Data for my Bayesian Network.
This is my current Code:

data = np.random.randint(2, size=(5, 6))
columns = ['p_1', 'p_2', 'OP1', 'OP2', 'OP3', 'OP4']
df = pd.DataFrame(data=data, columns=columns)

df.loc[(df['p_1'] == 1) & (df['p_2'] == 1), 'OP1'] = 1

df.loc[(df['p_1'] == 1) & (df['p_2'] == 0), 'OP2'] = 1

df.loc[(df['p_1'] == 0) & (df['p_2'] == 1), 'OP3'] = 1

df.loc[(df['p_1'] == 0) & (df['p_2'] == 0), 'OP4'] = 1


print(df)

So every time, for example, p_1 has a 1 and p_2 has a 1, the OP1 should be 1 as well, all the other values should output 0 in the column.
When p_1 is 1 and p_2 is 0, then OP2 should be 1 an d all others 0, and so on.

But my current Output is the following:

p_1p_2OP1OP2OP3OP4
000001
101111
001101
011111
100110

Is there any way to fix it? What did I do wrong?

I didn't really understand the solutions to other peoples questions, so I thought Id ask here.

I hope that someone can help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱已欠费 2025-01-25 05:04:39

问题是,当您实例化 df 时,“OP”列已经有了一些值:

data = np.random.randint(2, size=(5, 6)) 
columns = ['p_1', 'p_2', 'OP1', 'OP2', 'OP3', 'OP4'] 
df = pd.DataFrame(data=data, columns=columns) 

df                                                                      

   p_1  p_2  OP1  OP2  OP3  OP4
0    1    1    0    1    0    0
1    0    0    1    1    0    1
2    0    1    1    1    0    0
3    1    1    1    1    0    1
4    0    1    1    0    1    0

用代码修复它的一种方法是在之前将所有“OP”列强制为 0:

df["OP1"] = df["OP2"] = df["OP3"] df["OP4"] = 0    

但是您正在生成随机数太多。我会这样做:

data = np.random.randint(2, size=(5, 2)) 
columns = ['p_1', 'p_2'] 
df = pd.DataFrame(data=data, columns=columns) 
df["OP1"] = ((df['p_1'] == 0) & (df['p_2'] == 1)).astype(int)

The problem is that when you instantiate df, the "OP" columns already have some values:

data = np.random.randint(2, size=(5, 6)) 
columns = ['p_1', 'p_2', 'OP1', 'OP2', 'OP3', 'OP4'] 
df = pd.DataFrame(data=data, columns=columns) 

df                                                                      

   p_1  p_2  OP1  OP2  OP3  OP4
0    1    1    0    1    0    0
1    0    0    1    1    0    1
2    0    1    1    1    0    0
3    1    1    1    1    0    1
4    0    1    1    0    1    0

One way of fixing it with your code is forcing all "OP" columns to 0 before:

df["OP1"] = df["OP2"] = df["OP3"] df["OP4"] = 0    

But then you are generating too many random numbers. I'd do this instead:

data = np.random.randint(2, size=(5, 2)) 
columns = ['p_1', 'p_2'] 
df = pd.DataFrame(data=data, columns=columns) 
df["OP1"] = ((df['p_1'] == 0) & (df['p_2'] == 1)).astype(int)
短叹 2025-01-25 05:04:39

您可以定义用于测试的元组,并通过将掩码值转换为 inetegers 来创建新列,以将 True/False 映射到 1/0

vals = [(1,1),(1,0),(0,1),(0,0)]
for i, (a, b) in enumerate(vals, 1):
    df[f'OP{i}'] = ((df['p_1'] == a) & (df['p_2'] == b)).astype(int)
print(df)
   p_1  p_2  OP1  OP2  OP3  OP4
0    0    0    0    0    0    1
1    0    1    0    0    1    0
2    0    1    0    0    1    0
3    0    1    0    0    1    0
4    1    0    0    1    0    0

在您的解决方案中设置 0< /code> 首先,因为原始 DataFrame 中已经设置了 1 值:

cols = ['OP1', 'OP2', 'OP3', 'OP4']
df[cols] = 0

You can defined tuples for test and create new columns by casting values of mask to inetegers for True/False to 1/0 mapping:

vals = [(1,1),(1,0),(0,1),(0,0)]
for i, (a, b) in enumerate(vals, 1):
    df[f'OP{i}'] = ((df['p_1'] == a) & (df['p_2'] == b)).astype(int)
print(df)
   p_1  p_2  OP1  OP2  OP3  OP4
0    0    0    0    0    0    1
1    0    1    0    0    1    0
2    0    1    0    0    1    0
3    0    1    0    0    1    0
4    1    0    0    1    0    0

In your solution set 0 first, because already are set 1 values in original DataFrame:

cols = ['OP1', 'OP2', 'OP3', 'OP4']
df[cols] = 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文