我有一个使用熊猫和sklern的脚本gapminder1.py.
# TODO: Add import statements
import pandas as pd
from sklearn.linear_model import LinearRegression
# Assign the dataframe to this variable.
# TODO: Load the data
bmi_life_data = pd.read_csv("CSV_DATA/bmi_and_life_expectancy.csv")
print(bmi_life_data)
# Make and fit the linear regression model
#TODO: Fit the model and Assign it to bmi_life_model
bmi_life_model = LinearRegression()
bmi_life_model.fit(bmi_life_data[['BMI']], bmi_life_data[['Life expectancy']])
# Make a prediction using the model
# TODO: Predict life expectancy for a BMI value of 21.07931
laos_life_exp = bmi_life_model.predict(21.07931)
Run Code Online (Sandbox Code Playgroud)
我正在从cmd控制台运行该脚本,该工作正常但pycharm中的相同脚本向我显示错误
C:\Users\tripathi\AppData\Local\Continuum\anaconda3\envs\dsnd\python.exe C:/Users/tripathi/PycharmProjects/dsnd/gapminder1.py
Traceback (most recent …Run Code Online (Sandbox Code Playgroud) 我是正则表达式的新手,并尝试从基本上是文件路径的字符串中提取文件名。
string = "input_new/survey/argentina-attributes.csv"
string_required = argentina-attributes
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过下面的代码来做到这一点。
string.split('/')[2].split('.')[0]
Run Code Online (Sandbox Code Playgroud)
但我希望通过使用正则表达式来做到这一点,因此如果将来路径的格式发生变化(input_new/survey/path/path/argentina-attributes.csv)不应影响输出。
我知道以前问过类似的问题,但我正在寻找一种适合我的用例的模式。
我是 MinIo 的新手,我正在使用 minio python 库并尝试将熊猫数据框保存为 CSV。根据那里的文档,我使用put_object将数据插入远程云位置。下面是我的代码。
from minio import Minio
from minio.error import ResponseError
from io import StringIO, BytesIO
import pandas as pd
import os
minioClient = Minio('mydomain.com',
access_key='my_access_key',
secret_key='scret_key',
secure=False)
df = panda data frame
csv_buffer = StringIO()
df.to_csv(csv_buffer)
minioClient.put_object('mu_bucket',
'mypath/test.csv',
data = csv_buffer.getvalue(),
length = csv_buffer.tell(),
content_type='application/csv')
Run Code Online (Sandbox Code Playgroud)
在那里的文档中,所有示例都在保存物理文件,但我需要从数据帧中保存。这就是我使用 StringIO 创建字符串缓冲区的原因。但得到以下错误。
AttributeError: 'str' object has no attribute 'read
Run Code Online (Sandbox Code Playgroud)
非常感谢任何帮助谢谢。