我正在尝试使用函数替换数据框列“功能”中的 nan 值fillna()。我面临的问题如下:
isnull()dfcomp[dfcomp['Functional'].isnull()==True]

dfcomp['Functional'][2216]

fillna(),没有任何反应。即使在运行 fillna 语句之后,我也可以重新运行第一个语句并看到相同的 2 个 nan 实例。dfcomp['Functional']=dfcomp['Functional'].fillna(value=dfcomp['Functional'].mode())
顺便说一句我已经尝试过两个版本
dfcomp['Functional'].fillna(value=dfcomp['Functional'].mode(),inplace=True)

replace()该功能,但没有运气dfcomp['Functional']=dfcomp['Functional'].replace({'nan':dfcomp['Functional'].mode()})
我的代码有问题吗?为什么fillna()不认识到什么nan时候isnull()可以这样做呢?另外,为什么索引搜索显示的值是,但是当我尝试使用没有结果nan替换相同的值时?replace()
fillna()当我无法识别nan 值时,如何替换它?
我想根据另一列的值对一列上的 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_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)
有没有办法根据另一列的值对 DataFrame 的单元格进行着色?
在 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)
如何控制对齐?
我有一个标准数据框,如下所示:
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) 我正在构建一个基本的 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_passwords,
顺便说一句,我使用了官方文档中描述的相同代码:
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)