我有这样的代码:
def func(df):
return df.column[0]
Run Code Online (Sandbox Code Playgroud)
我正在运行pylint并且它一直给我这个消息,因为它将df标记为无效的名称,尽管它是常规.
C:210, 9: Invalid variable name "df" (invalid-name)
Run Code Online (Sandbox Code Playgroud)
其中210指的是行号(不是消息代码)
Pylint似乎说我可以按ID类型排除邮件,但是:
我将日期时间格式的数据导出到csv.当我将其导回时,我需要能够在没有任何列名或列号引用的情况下以日期读取数据.
看起来像Pandas read_csv有自动将日期解析为日期时间格式的选项,但它似乎没有在这里工作.
# Create date data
df_list = [['2014-01-01','2014-02-01'],['2015-01-01','2015-02-01']]
df = pd.DataFrame(df_list,columns=['date1','date2'])
# Convert to datetime format
df['date1'] = pd.to_datetime(df['date1'])
# Export to csv
df.to_csv('_csv_file.csv',index=False)
# Read in the data and parse dates
in_df = pd.read_csv('_csv_file.csv',parse_dates=True,infer_datetime_format=True)
# Dates are not of correct type
print df.dtypes
print
print in_df.dtypes
Out [1]:
date1 datetime64[ns]
date2 object
dtype: object
date1 object
date2 object
dtype: object
Run Code Online (Sandbox Code Playgroud)
有没有办法在导入时自动解析日期列而不明确标识列名称或位置?
我有两个阶段的数据:
import numpy as np
data_pre = np.array([[1., 2., 203.],
[0.5, np.nan, 208.]])
data_post = np.array([[2., 2., 203.],
[0.5, 2., 208.]])
Run Code Online (Sandbox Code Playgroud)
我还有两个预先存在的拟合估算器:
from sklearn.preprocessing import Imputer
from sklearn.ensemble import GradientBoostingRegressor
imp = Imputer(missing_values=np.nan, strategy='mean', axis=1).fit(data_pre)
gbm = GradientBoostingRegressor().fit(data_post[:,:2], data_post[:,2])
Run Code Online (Sandbox Code Playgroud)
我需要将一个合适的管道传递data_pre给另一个函数。
def the_function_i_need(estimators):
"""
"""
return fitted pipeline
fitted_pipeline = the_function_i_need([imp, gbm])
sweet_output = static_function(fitted_pipeline, data_pre)
Run Code Online (Sandbox Code Playgroud)
有没有办法将这两个现有的和拟合的模型对象组合到拟合的管道中,而无需重新拟合模型,还是我运气不好?
我有一个要转换为百分位数的数组。例如,假设我有一个正态分布的数组:
import numpy as np
import matplotlib.pyplot as plt
arr = np.random.normal(0, 1, 1000)
plt.hist(arr)
Run Code Online (Sandbox Code Playgroud)
对于该数组中的每个值,我想计算该值的百分位数(例如0是上述分布的第50个百分位数,所以0-> 0.5)。由于每个百分位数应具有相等的权重,因此结果应均匀分布。
我发现了,np.percentile但此函数返回给定数组和分位数的值,而我需要的是返回给定数组和值的分位数。
有相对有效的方法吗?
我想检查一个字段是否包含一个字符串。
我想要一个看起来像这样的函数:
FIND("string_to_find",field_to_search)
Run Code Online (Sandbox Code Playgroud)
我的数据如下所示:
field_to_search
---------------
"no match in this string"
"record 2 has no matches"
"ahh finally xxxstring_to_findxxx is here"
Run Code Online (Sandbox Code Playgroud)
我正在寻找一个函数来标识包含指定的字符串以及字符串在什么位置开始。
return
------
-1
-1
15
Run Code Online (Sandbox Code Playgroud) 我有三本词典:
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'b': 3, 'c': 4}
dict3 = {'c': 4, 'd': 4}
Run Code Online (Sandbox Code Playgroud)
我想将它们"合并"到列表中
merged_dict = {'a':[1, np.nan, np.nan],
'b':[2, 3, np.nan],
'c':[3, 4, 4],
'd':[np.nan, np.nan, 4]}
Run Code Online (Sandbox Code Playgroud)
某些字典中有一些键而不是其他字典,这使得循环很麻烦.想知道最干净的方法是什么.
我正在使用seaborn并且想要生成一个盒子图,其中所有盒子都具有相同的颜色.出于某种原因,seaborn为每个盒子使用不同的颜色,并且没有选项来停止这种行为并为所有盒子设置相同的颜色.
我怎样才能强迫seaborn为所有盒子使用相同的颜色?
fig, ax = plt.subplots(figsize=(10, 20))
sns.boxplot(y='categorical_var', x='numeric_var', ax=ax)
Run Code Online (Sandbox Code Playgroud) 我从树图开始:
df = pd.DataFrame([1,20,3],[2,30,4],[3,40,5],columns=['mean','size','stat'])
fig,[ax1,ax2,ax3] = plt.subplots(1, 3, sharey=True)
ax1.barh(np.arange(len(df)),df['mean'].values, align='center')
ax2.barh(np.arange(len(df)),df['size'].values, align='center')
ax3.barh(np.arange(len(df)),df['stat'].values, align='center')
Run Code Online (Sandbox Code Playgroud)
有没有办法在所有三个图上旋转x轴标签?
我有一个带有 15 个轴标签的图,Pandas 自动默认显示所有其他类别名称。如何让它在不跳过项目的情况下显示所有 x 轴标签?
rows = []
for x in range(0,14):
rows.append(['a',14-x])
df = pd.DataFrame(rows)
df = df.set_index(0)
df.plot(xticks=df.index)
Run Code Online (Sandbox Code Playgroud) 简单的问题,但我在任何地方都找不到答案,因此其他人可能会遇到同样的挫败感。
在 Hive 中,如何计算 field1**field2。
我有一个数组,可以给我一些其他数字的累积百分比:
my_cumulative_array = np.asarray(range(0,50))/float(50)
Run Code Online (Sandbox Code Playgroud)
我想将此数组分成n组,每组相等的权重:
chunks = [[row indexes 01-10], #First 20% based on order
[row indexes 11-20], #Second 20% based on order
[row indexes 21-30],
[row indexes 31-40],
[row indexes 41-50]]
Run Code Online (Sandbox Code Playgroud)
似乎应该有一个聪明的方法可以有效地做到这一点。
python ×7
matplotlib ×3
numpy ×3
pandas ×3
hive ×2
arrays ×1
boxplot ×1
colors ×1
csv ×1
date ×1
dictionary ×1
find ×1
merge ×1
percentile ×1
pipeline ×1
pylint ×1
scikit-learn ×1
seaborn ×1