我想使用此数据框的geom_bar()创建一个并排的条形图,
> dfp1
value percent1 percent
1 (18,29] 0.20909091 0.4545455
2 (29,40] 0.23478261 0.5431034
3 (40,51] 0.15492958 0.3661972
4 (51,62] 0.10119048 0.1726190
5 (62,95] 0.05660377 0.1194969
Run Code Online (Sandbox Code Playgroud)
使用x轴上的值和百分比作为并排条形图.我试过用这个代码,
p = ggplot(dfp1, aes(x = value, y= c(percent, percent1)), xlab="Age Group")
p = p + geom_bar(stat="identity", width=.5)
Run Code Online (Sandbox Code Playgroud)
但是,我收到此错误:错误:美学必须是长度为1或与dataProblems:value相同的长度.我的百分比和百分比1与值的长度相同,所以我很困惑.谢谢您的帮助.
我有一个非常基本的问题。我正在使用熊猫数据框进行绘制,但是我想在某些日期周围添加突出显示。
In[122]:
df1_99.plot(x='date', y='units', ylim=[0,11], figsize=[12,12])
Run Code Online (Sandbox Code Playgroud)
出[122]:
我在stackoverflow上发现了此代码以添加突出显示。
fig, ax = plt.subplots()
ax.plot_date(t, y, 'b-')
ax.axvspan(*mdates.datestr2num(['10/27/2011', '11/2/2011']), color='red', alpha=0.5)
fig.autofmt_xdate()
plt.show()
Run Code Online (Sandbox Code Playgroud)
我的问题是如何在当前代码中使用ax.avxspan?还是我需要将x ='date'和y ='units'转换为numpy数组并使用上面代码中的格式?
到目前为止,这是我的代码:
year = 2012
start = datetime.date(year, 1, 1)
end = start + pd.offsets.MonthEnd(72)
class FrBusinessCalendar(AbstractHolidayCalendar):
""" Custom Holiday calendar
"""
rules = [
Holiday('New Years Day', month=1, day=1),
#USThanksgivingDay,
Holiday('Fourth Of July', month=7, day=4),
Holiday('Thanksgiving', month=11, day=1, offset=DateOffset(weekday=TH(4))),
Holiday('Black Friday', month=11, day=1, offset=pd.DateOffset(weekday=FR(4))),
Holiday("Cyber Monday", month=11, day=1, offset=[pd.DateOffset(weekday=SA(4)), pd.DateOffset(2)]),
Holiday('Christmas Day', month=12, day=25)
]
cal = FrBusinessCalendar()
# Getting the holidays (off-days) between two dates
cal.holidays(start=start, end=end)
Run Code Online (Sandbox Code Playgroud)
现在,我想要做的是将所有这些日期放入一个数据框中,其中假日名称作为一列,日期作为另一列。所以对于我的例子...
date holiday
2012-01-01 New Years Day
2012-07-04 Fourth Of July
2012-11-22 …
Run Code Online (Sandbox Code Playgroud) 我有这个数据帧:
dftrain
date store_nbr item_nbr units
0 2012-01-01 1 1 0
1 2012-01-01 1 2 0
2 2012-01-01 2 3 0
3 2012-01-01 3 4 0
4 2012-01-01 4 5 0
...
Run Code Online (Sandbox Code Playgroud)
而这个数据帧:
dfkey
store_nbr station_nbr
0 1 1
1 2 1
2 3 4
3 4 1
Run Code Online (Sandbox Code Playgroud)
我想添加一个列dftrain,其中station_nbr列将商店编号与站号相匹配.
dftrain
date store_nbr item_nbr units station_nbr
0 2012-01-01 1 1 0 1
1 2012-01-01 1 2 0 1
2 2012-01-01 2 3 0 1
3 2012-01-01 3 4 0 4
4 …
Run Code Online (Sandbox Code Playgroud) 我想将此正则表达式模式复制到 regexp_substr。我想捕获第二组。
'(\?)(.*?)(&|$)'
Run Code Online (Sandbox Code Playgroud)
我试过这个
regexp(my_url, '\\?.*?&|$')
Run Code Online (Sandbox Code Playgroud)
以及上面的一些类似的变体,但我一直收到错误:
ERROR: XX000: Invalid preceding regular expression prior to repetition operator. The error occured while parsing the regular expression: '\?.*?>>>HERE>>>&|$'.
我有这个数据透视表:
[in]:unit_d
[out]:
units
store_nbr item_nbr
1 9 27396
28 4893
40 254
47 2409
51 925
89 157
93 1103
99 492
2 5 55104
11 655
44 117125
85 106
93 653
Run Code Online (Sandbox Code Playgroud)
我想要一个字典,其中'store_nbr'作为键,'item_nbr'作为值.
所以,{'1': [9, 28, 40,...,99], '2': [5, 11 ,44, 85, 93], ...}
我有这个数据框:
dfexample = pd.DataFrame({'OID' : [7, 7, 7, 7],
'Category' : ['Plumbing', 'Plumbing', 'Plumbing', 'Plumbing'],
'Product_Type' : ['Accessory', 'Shower Accessories', 'Showers', 'Showers'],
'Extended_Price' : [20.5, 12.12, 122.45, 225.98]})
Run Code Online (Sandbox Code Playgroud)
我想按“ OID”对数据框进行分组,并获取对象列(Category
和Product_Type
)的“第一个”字符串,因此在这种情况下为'plumbing'
和'accessory'
。所需输出:
[in]:dfgrouped
[out]:
OID Category Extended_Price Product_Type
7 Plumbing 381.05 Accessory
Run Code Online (Sandbox Code Playgroud)
现在,我将所有字符串连接在一起,并且不返回“第一个”字符串。
def f(x):
return pd.Series(dict(Category = x['Category'].sum(),
Extended_Price = x['Extended_Price'].sum(),
Product_Type = x['Product_Type'].sum()
))
dfexample.groupby('OID').apply(f)
Run Code Online (Sandbox Code Playgroud) 为什么这两个代码块没有做同样的事情?
category = None
prodid_brand = None
prod_type = None
prod_application = None
prod_handletype = None
prod_series = None
Run Code Online (Sandbox Code Playgroud)
我想通过执行以下操作来"清理"我的代码,但它与上面的代码不同.
column_list = [category, prodid_brand, prod_type, prod_application,
prod_handletype, prod_series]
for col in column_list:
col = None
Run Code Online (Sandbox Code Playgroud)
还有一种"更清洁"的方式来实例化除顶部代码块之外的所有变量.
python ×6
pandas ×5
calendar ×1
dataframe ×1
dictionary ×1
ggplot2 ×1
matplotlib ×1
numpy ×1
pivot-table ×1
r ×1
regex ×1