我有一个数据框,每列的格式都不同。我需要将其导出到 csv 或 dat 文件。但收到以下错误消息:
AttributeError:“Styler”对象没有属性“to_csv”
如何解决这个问题?
import pandas as pd
import datetime
def time_formatter(data):
return datetime.datetime.strptime(data, "%Y/%m/%d").date().strftime('%Y%m%d')
df = pd.DataFrame({'a':[1,2,3], 'b':['2017/01/01', '2017/01/02','2016/12/31'], 'c':['aaa', 'bbb', 'ccc'], 'd':[4,5,6]})
formatter = {'a':'{:4.2f}', 'b': time_formatter, 'd':'{:8.2f}'}
df = df.style.format(formatter)
df.to_csv('aaa.csv')
Run Code Online (Sandbox Code Playgroud) 是否可以在 iPython 控制台中显示pandas 样式?Jupyter 笔记本中的以下代码
import pandas as pd
import numpy as np
np.random.seed(24)
df = pd.DataFrame({'A': np.linspace(1, 10, 5)})
df = pd.concat([df, pd.DataFrame(np.random.randn(5, 1), columns=list('B'))],
axis=1)
df.style.format({'B': "{:.2%}"})
Run Code Online (Sandbox Code Playgroud)
正确产生
在控制台中我只得到
In [294]: df.style.format({'B': "{:.2%}"})
Out[294]: <pandas.io.formats.style.Styler at 0x22f3f4fe780>
Run Code Online (Sandbox Code Playgroud)
是否可以在这里实现类似的结果,或者样式引擎是否依赖于 html 前端?
预先感谢您的任何帮助。
由于用户在界面中引入数据,我有一个类似字符串查询的结果。
query = '(ColA=="7") & (ColB=="3") & (ColC=="alpha") & (ColD=="yu")'
Run Code Online (Sandbox Code Playgroud)
现在我想根据这些条件更新 df 的列,为其分配变量 Z。
我不知道是否可以通过某种方式完成loc
df.loc[query, 'ColZ'] = Z
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 python 从 blob 存储读取多个 CSV 文件。
我正在使用的代码是:
blob_service_client = BlobServiceClient.from_connection_string(connection_str)
container_client = blob_service_client.get_container_client(container)
blobs_list = container_client.list_blobs(folder_root)
for blob in blobs_list:
blob_client = blob_service_client.get_blob_client(container=container, blob="blob.name")
stream = blob_client.download_blob().content_as_text()
Run Code Online (Sandbox Code Playgroud)
我不确定存储在 pandas 数据框中读取的 CSV 文件的正确方法是什么。
我尝试使用:
df = df.append(pd.read_csv(StringIO(stream)))
Run Code Online (Sandbox Code Playgroud)
但这向我显示了一个错误。
知道我该怎么做吗?
我正在尝试仅对某个输出进行着色,如下所示
df['a'] = df['a'].fillna(df['b'].map(s)).style.applymap(lambda x: "background-color:yellow")
Run Code Online (Sandbox Code Playgroud)
所以我得到了错误
AttributeError: 'Series' object has no attribute 'style'
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到正确并且仅对由此产生的输出进行着色?
我需要将许多列(4000)添加到 pyspark 的数据框中。我正在使用该withColumn函数,但出现断言错误。
df3 = df2.withColumn("['ftr' + str(i) for i in range(0, 4000)]", [expr('ftr[' + str(x) + ']') for x in range(0, 4000)])
Run Code Online (Sandbox Code Playgroud)
不知道出了什么问题。
在确保自制软件可以在其上运行后,我刚刚购买了一台带有 M1 芯片的新 Macbook Pro。我可以 pip 安装除 mysqlclient 之外的任何其他库,并且通过自制程序安装它没有问题,所以我想从下面的错误消息中看出,这是 mysqlclient 未更新为最新版本 Big 的问题吗? Sur,或者这是新 Mac 本身的问题?只要知道这对我来说就是一个很好的开始,如果您对如何解决它有建议,那就更好了!谢谢你!
ERROR: Command errored out with exit status 1:
command: /Users/danieljonathanschaefer/Desktop/work/401GOVE/bin/python3 -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/ht/p0kw7_d11ns61kqmpmfcy6b40000gn/T/pip-install-bifqtsix/mysqlclient_6f9d0caea513420ea2052d60956aab6d/setup.py'"'"'; __file__='"'"'/private/var/folders/ht/p0kw7_d11ns61kqmpmfcy6b40000gn/T/pip-install-bifqtsix/mysqlclient_6f9d0caea513420ea2052d60956aab6d/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/ht/p0kw7_d11ns61kqmpmfcy6b40000gn/T/pip-record-ukcglom_/install-record.txt --single-version-externally-managed --compile --install-headers /Users/danieljonathanschaefer/Desktop/work/401GOVE/include/site/python3.9/mysqlclient
cwd: /private/var/folders/ht/p0kw7_d11ns61kqmpmfcy6b40000gn/T/pip-install-bifqtsix/mysqlclient_6f9d0caea513420ea2052d60956aab6d/
Complete output (43 lines):
mysql_config --version
['8.0.25']
mysql_config --libs
['-L/opt/homebrew/Cellar/mysql/8.0.25_1/lib', '-lmysqlclient', '-lz', '-lzstd', '-lssl', '-lcrypto', '-lresolv'] …Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的 jquery 滑块;它只支持一个滑块,我想让它支持同一页面上的多个滑块。
\n示例:同一页面上的滑块 1、滑块 2、滑块 3 等。
\n工作片段在这里是工作示例。
\n我知道我必须使用每个函数\xe2\x80\x94并且我做了\xe2\x80\x94但它停止工作。我不知道我哪里错了。这就是我尝试过的;代码太长,所以我缩短了它:
\n$(\'#slider\').each(function() {\n var current_slider = $(this);\n //slider codes in here \n});\nRun Code Online (Sandbox Code Playgroud)\n这是我的完整代码:
\n$(function(){ \nvar Slider = 0;\n$.Slider = function(total){\n $("#indicator li").removeClass("active");\n $("#image li").hide();\n if (Slider < total -1){\n Slider++;\n $("#indicator li:eq("+Slider+")").addClass("active");\n $("#image li:eq("+Slider+")").fadeIn("slow"); \n }else {\n $("#indicator li:first").addClass("active");\n $("#image li:first").fadeIn("slow"); \n Slider = 0; \n }\n }\n\n var totalLi = $("#indicator li").length;\n var interval = setInterval(\'$.Slider(\'+totalLi+\')\',5000);\n $("#slider").hover(function(){\n clearInterval(interval);\n },function(){\n interval = setInterval(\'$.Slider(\'+totalLi+\')\',5000); …Run Code Online (Sandbox Code Playgroud) 当我尝试使用下一个身份验证创建不和谐的 oauth 客户端并单击“登录”时,出现以下错误:
https://next-auth.js.org/errors#get_authorization_url_error client_id is required {
message: 'client_id is required',
stack: 'TypeError: client_id is required\n' +
' at new BaseClient (/Users/muyuan/Documents/minibot/node_modules/openid-client/lib/client.js:178:13)\n' +
' at new Client (/Users/muyuan/Documents/minibot/node_modules/openid-client/lib/client.js:1823:7)\n' +
' at openidClient (/Users/muyuan/Documents/minibot/node_modules/next-auth/core/lib/oauth/client.js:28:18)\n' +
' at getAuthorizationUrl (/Users/muyuan/Documents/minibot/node_modules/next-auth/core/lib/oauth/authorization-url.js:67:51)\n' +
' at Object.signin (/Users/muyuan/Documents/minibot/node_modules/next-auth/core/routes/signin.js:37:60)\n' +
' at NextAuthHandler (/Users/muyuan/Documents/minibot/node_modules/next-auth/core/index.js:191:39)\n' +
' at processTicksAndRejections (internal/process/task_queues.js:95:5)\n' +
' at async NextAuthNextHandler (/Users/muyuan/Documents/minibot/node_modules/next-auth/next/index.js:21:19)\n' +
' at async /Users/muyuan/Documents/minibot/node_modules/next-auth/next/index.js:57:32\n' +
' at async Object.apiResolver (/Users/muyuan/Documents/minibot/node_modules/next/dist/server/api-utils.js:101:9)',
name: 'TypeError'
}
[next-auth][error][SIGNIN_OAUTH_ERROR]
https://next-auth.js.org/errors#signin_oauth_error client_id is required { …Run Code Online (Sandbox Code Playgroud)