小编Tim*_*ess的帖子

为什么 fillna with mode 不替换数据框中的 nan 值?

我正在尝试使用函数替换数据框列“功能”中的 nan 值fillna()。我面临的问题如下:

  1. 我可以使用以下方法检测空值isnull()

dfcomp[dfcomp['Functional'].isnull()==True]

搜索空值

  1. 使用上面的索引我搜索了实际值

dfcomp['Functional'][2216]

使用索引进行值搜索

  1. 但是当我尝试使用 填充 nan 时fillna(),没有任何反应。即使在运行 fillna 语句之后,我也可以重新运行第一个语句并看到相同的 2 个 nan 实例。

dfcomp['Functional']=dfcomp['Functional'].fillna(value=dfcomp['Functional'].mode())

顺便说一句我已经尝试过两个版本

dfcomp['Functional'].fillna(value=dfcomp['Functional'].mode(),inplace=True)

fillna()

  1. 我也尝试使用replace()该功能,但没有运气

dfcomp['Functional']=dfcomp['Functional'].replace({'nan':dfcomp['Functional'].mode()})

我的代码有问题吗?为什么fillna()不认识到什么nan时候isnull()可以这样做呢?另外,为什么索引搜索显示的值是,但是当我尝试使用没有结果nan替换相同的值时?replace()

fillna()当我无法识别nan 值时,如何替换它?

python nan dataframe pandas fillna

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

基于其他列值的 pandas 颜色单元格

我想根据另一列的值对一列上的 DataFrame 的红色单元格进行着色。

这是一个例子:

df = pd.DataFrame([
  { 'color_A_in_red': True , 'A': 1 },
  { 'color_A_in_red': False , 'A': 2 },
  { 'color_A_in_red': True , 'A': 2 },
])
Run Code Online (Sandbox Code Playgroud)

应该给出: 彩色_df

我知道如何将 df 的单元格着色为红色,但仅基于该单元格的值,而不是另一个单元格的值:

df_style = df.style
df_style.applymap(func=lambda x: 'background-color: red' if x == 2 else None, subset=['A'])
df_style
Run Code Online (Sandbox Code Playgroud)

拧色_df

有没有办法根据另一列的值对 DataFrame 的单元格进行着色?

python dataframe pandas pandas-styles

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

MultiIndex 列名称和 pandas styler 对齐

在 Jupyter 中,当使用 来显示 DataFrame 时MultiIndex,第一层是左对齐的。

import numpy as np
cols = pd.MultiIndex.from_tuples([(x, y) for x in ['A', 'B', 'C'] for y in ['O', 'I']])
df = pd.DataFrame(np.random.randn(2, 6), index=['n', 'm'], columns=cols)
df
Run Code Online (Sandbox Code Playgroud)

多索引左对齐

但是,当使用 pandas styler 显示相同内容时,它会右对齐:

df.style
Run Code Online (Sandbox Code Playgroud)

多索引 pandas styler 右对齐

如何控制对齐?

multi-index pandas pandas-styles

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

如何制作具有多个聚合的自定义分组数据框

我有一个标准数据框,如下所示:

       Id Type  Speed Efficiency Durability
0   Id001    A     OK         OK      nonOK
1   Id002    A  nonOK         OK      nonOK
2   Id003    B  nonOK      nonOK      nonOK
3   Id004    B  nonOK      nonOK         OK
4   Id005    A  nonOK      nonOK         OK
5   Id006    A     OK         OK         OK
6   Id007    A     OK      nonOK         OK
7   Id008    B  nonOK      nonOK         OK
8   Id009    C     OK         OK         OK
9   Id010    B     OK         OK      nonOK
10  Id011    C     OK      nonOK         OK
11  Id012    C     OK      nonOK         OK
12  Id013 …
Run Code Online (Sandbox Code Playgroud)

python pandas

4
推荐指数
1
解决办法
95
查看次数

Streamlit Authenticate.__init__() 获得参数“cookie_expiry_days”的多个值

我正在构建一个基本的 Streamlit 应用程序。为了验证用户凭据,我使用了Streamlit身份验证模块。只有一个问题,代码不起作用!

错误

类型错误:验证。init () 获得参数 'cookie_expiry_days' 的多个值

追溯:

文件“C:\Users_M92\Desktop\Coding\Python\Projects\Personal1\venv\lib\site-packages\streamlit\scriptrunner\script_runner.py”,第 557 行,在 _run_script exec(code, module.dict )

文件“app.py”,第 23 行,authenticator = stauth.Authenticate(names, usernames, hashed_pa​​sswords,

顺便说一句,我使用了官方文档中描述的相同代码:

代码

import streamlit as st
import streamlit_authenticator as stauth

names = ['John Smith', 'Rebecca Briggs']
usernames = ['jsmith', 'rbriggs']
passwords = ['123', '456']

hashed_passwords = stauth.Hasher(passwords).generate()

authenticator = stauth.Authenticate(names, usernames, hashed_passwords,
    'some_cookie_name', 'some_signature_key', cookie_expiry_days=30)

name, authentication_status, username = authenticator.login('Login', 'main')

if authentication_status:
    authenticator.logout('Logout', 'main')
    st.write('Welcome *%s*' % (name)) …
Run Code Online (Sandbox Code Playgroud)

python visual-studio-code streamlit

2
推荐指数
1
解决办法
7303
查看次数