小编MaM*_*aMo的帖子

Python:使用相对路径导入excel文件

我尝试导入一个与脚本不在同一文件夹中的 Excel 文件。我需要获取上面的一个文件夹,然后进入另一个文件夹(B_folder),并且有文件 2_file.xlsx

我试过:

df = pd.read_excel(r'..\B_folder\2_file.xlsx')
Run Code Online (Sandbox Code Playgroud)

并得到:

FileNotFoundError: [Errno 2] No such file or directory: '..\\B_folder\\2_file.xlsx'
Run Code Online (Sandbox Code Playgroud)

还尝试过:

  • 前斜杠而不是反斜杠

  • 路径前没有“r”

但我总是收到上面或下面的错误消息:

OSError: [Errno 22] Invalid argument: '..\\B_folder\2_file.xlsx'
Run Code Online (Sandbox Code Playgroud)

怎么了?

python import-from-excel importerror pandas

7
推荐指数
1
解决办法
3万
查看次数

Python:使用 np.where 维护多列

是否可以使用同时维护多个列np.where?通常,一列是用 维护的np.where,所以我的编码如下所示:

df['col1'] = np.where(df[df.condition == 'yes'],'sth', '')
df['col2'] = np.where(df[df.condition == 'yes'], 50.00, 0.0)
Run Code Online (Sandbox Code Playgroud)

但由于我对相同的条件进行了两次测试,我想知道是否可以通过 2 列并在一次运行中填充它们。

我试过这个:

df['col1','col2'] = np.where(df[df.condition == 'yes'],['sth',50.00], ['',0.0])
Run Code Online (Sandbox Code Playgroud)

但这不起作用。有没有办法实现这一点?

python dataframe pandas

5
推荐指数
1
解决办法
4690
查看次数

Python:计算列中每个值有效的概率

我有一个像这样的熊猫数据框:

+-----+----------+
| No  | quantity |
+-----+----------+
|   1 |    100.0 |
|   2 |    102.3 |
|   3 |    301.2 |
|   4 |    100.6 |
|   5 |    120.9 |
| ... |      ... |
+-----+----------+
Run Code Online (Sandbox Code Playgroud)

我如何计算每个值适合数据集的概率(在数据框中,除了第 3 个值之外)。这个想法是使用标准化正态分布并计算出现某个值(或更极端的值)的概率。在这种情况下,第 3 号发生的概率几乎为零,因为它与所有其他值相距甚远。

我知道如何在纸上对每个值执行此操作:

  1. 计算 z 分数

  2. 在标准正态概率表中找到相应的值

  3. 如果值低于分布的平均值,则概率为 1-概率

所以期望的输出是这样的:

+-----+----------+--------+
| No  | quantity |  prob  |
+-----+----------+--------+
|   1 |    100.0 | 99,85% |
|   2 |    102.3 | 99,81% |
|   3 |    301.2 | …
Run Code Online (Sandbox Code Playgroud)

python statistics probability pandas

3
推荐指数
1
解决办法
7764
查看次数