我将使用ggplot中的钻石数据集来说明我的观点,我想绘制一个直方图的价格,但我想显示每个切割的每个切割的计数这是我的代码
ggplot(aes(x = price ) , data = diamonds_df) +
geom_histogram(aes(fill = cut , binwidth = 1500)) +
stat_bin(binwidth= 1500, geom="text", aes(label=..count..) ,
vjust = -1) +
scale_x_continuous(breaks = seq(0 , max(stores_1_5$Weekly_Sales) , 1500 )
, labels = comma)
Run Code Online (Sandbox Code Playgroud)
这是我目前的情节

但是当你看到数字显示每个箱子上所有切口的数量时,我想显示每个箱子上每个切口的数量.
如果我能够配置Y轴而不是在步骤5000显示数字我可以手动配置的其他东西也是一个奖励点
给定已编制索引的现有Dataframe.
>>> df = pd.DataFrame(np.random.randn(10, 5),columns=['a', 'b', 'c', 'd', 'e'])
>>> df
a b c d e
0 -0.131666 -0.315019 0.306728 -0.642224 -0.294562
1 0.769310 -1.277065 0.735549 -0.900214 -1.826320
2 -1.561325 -0.155571 0.544697 0.275880 -0.451564
3 0.612561 -0.540457 2.390871 -2.699741 0.534807
4 -1.504476 -2.113726 0.785208 -1.037256 -0.292959
5 0.467429 1.327839 -1.666649 1.144189 0.322896
6 -0.306556 1.668364 0.036508 0.596452 0.066755
7 -1.689779 1.469891 -0.068087 -1.113231 0.382235
8 0.028250 -2.145618 0.555973 -0.473131 -0.638056
9 0.633408 -0.791857 0.933033 1.485575 -0.021429
>>> df.set_index("a")
b …Run Code Online (Sandbox Code Playgroud) 我正在使用jest来测试我的反应组件,我正在expect(...).toBeCalledWith(...);测试是否使用特定参数调用了一个函数,并且它可以与值类型一起使用.
问题是我想测试一个将对象作为参数的函数,所以当你调用expect(myFunc).toBeCalledWith(object);测试时总是失败,因为当然两个对象相互比较没有相同的引用.
那我怎么解决这个问题呢?
我要测试的示例代码是
it('the function should be called with the correct object', () => {
api.submitForm = jest.fn().mockReturnValue(Promise.resolve());
const wrapper = shallow(<component />);
const instance = wrapper.instance();
instance.submitForm();
const object = {
foo : 'foo',
bar: 'bar'
};
// this always fails even the function is called with the same object values
expect(api.submitForm).toBeCalledWith(object);
});
Run Code Online (Sandbox Code Playgroud)
错误消息将是这样的
Expected mock function to have been called with:
[{"bar": "bar", "foo": "foo"}]
But it was called with:
[{"bar": "bar", "foo": …Run Code Online (Sandbox Code Playgroud) 为了简单起见,假设我在数据框中有一个列,如下所示,每行包含多个国家/地区 ,
df <- data.frame(
countries = c(
"UK , Spain , Germany , Italy , Netherlands" ,
"UK , Canada , AUS , China" ,
"Spain , AUS , Italy , Russia"
)
)
Run Code Online (Sandbox Code Playgroud)
这就是数据的样子
countries
1 UK , Spain , Germany , Italy , Netherland
2 UK , Canada , AUS , China
3 Spain , AUS , Italy , Russia
Run Code Online (Sandbox Code Playgroud)
我们怎样才能将它变成类似的东西
countries
1 UK
2 Spain
3 Germany
4 Italy
5 Netherlands
6 UK
7 Canada
8 …Run Code Online (Sandbox Code Playgroud) 我有这个数据帧df:
U,Datetime
01,2015-01-01 20:00:00
01,2015-02-01 20:05:00
01,2015-04-01 21:00:00
01,2015-05-01 22:00:00
01,2015-07-01 22:05:00
02,2015-08-01 20:00:00
02,2015-09-01 21:00:00
02,2014-01-01 23:00:00
02,2014-02-01 22:05:00
02,2015-01-01 20:00:00
02,2014-03-01 21:00:00
03,2015-10-01 20:00:00
03,2015-11-01 21:00:00
03,2015-12-01 23:00:00
03,2015-01-01 22:05:00
03,2015-02-01 20:00:00
03,2015-05-01 21:00:00
03,2014-01-01 20:00:00
03,2014-02-01 21:00:00
Run Code Online (Sandbox Code Playgroud)
通过由U与一个Datetime对象.我想做的是过滤U几个月/年至少连续三次出现的值.到目前为止,我已经通过分组U,year并month为:
m = df.groupby(['U',df.index.year,df.index.month]).size()
Run Code Online (Sandbox Code Playgroud)
获得:
U
1 2015 1 1
2 1
4 1
5 1
7 1
2 2014 1 1
2 1
3 1 …Run Code Online (Sandbox Code Playgroud) 我试图编写一些代码,用逗号分隔数据帧列中的字符串(因此它成为一个列表),并从该列表中删除某个字符串(如果存在).删除不需要的字符串后,我想在逗号处再次加入列表元素.我的数据框看起来像这样:
df:
Column1 Column2
0 a a,b,c
1 y b,n,m
2 d n,n,m
3 d b,b,x
Run Code Online (Sandbox Code Playgroud)
所以基本上我的目标是从column2中删除所有b值,以便我得到:
DF:
Column1 Column2
0 a a,c
1 y n,m
2 d n,n,m
3 d x
Run Code Online (Sandbox Code Playgroud)
我写的代码如下:
df=df['Column2'].apply(lambda x: x.split(','))
def exclude_b(df):
for index, liste in df['column2].iteritems():
if 'b' in liste:
liste.remove('b')
return liste
else:
return liste
Run Code Online (Sandbox Code Playgroud)
第一行将列中的所有值拆分为逗号分隔列表.现在,我尝试迭代所有列表并删除b(如果存在),如果不存在则返回列表.如果我在末尾打印'liste',它只返回Column2的第一行,而不返回其他行.我究竟做错了什么?是否有办法将我的if条件实现为lambda函数?
我下面有一个 pandas 数据框 df
df = pd.DataFrame({'id':[1,2,3],'v' : ['r','r','i'], 'w' : ['r','r','i'],'x' : ['r','i','i']})
df
id v w x
1 r r r
2 r r i
3 i i i
Run Code Online (Sandbox Code Playgroud)
列的值为r和i。我想按行计算r和 的出现次数,并i生成另外两个列标题,并将 和 i` 的计数作为每行的值,我期望的最终结果如下rir
id v w x r i
1 r r r 3 0
2 i r r 2 1
3 i i i 0 3
Run Code Online (Sandbox Code Playgroud) 如何以不同的方式对正面和负面值进行求和pandas并将它们放在positive和negative列中?
我有这样的数据框如下:
df = pandas.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'],
'B' : ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'],
'C' : np.random.randn(8), 'D' : np.random.randn(8)})
Run Code Online (Sandbox Code Playgroud)
输出如下:
df
A B C D
0 foo one 0.374156 0.319699
1 bar one -0.356339 -0.629649
2 foo two -0.390243 -1.387909
3 bar three -0.783435 -0.959699
4 foo two -1.268622 -0.250871
5 bar two -2.302525 -1.295991
6 foo one -0.968840 1.247675
7 foo three …Run Code Online (Sandbox Code Playgroud) 我有以下类型的pandas.DataFrame:
sales_with_missing = pd.DataFrame({'month':[1,2,3,6,7,8,9,10,11,12],'code':[111]*10, 'sales':[np.random.randint(1500) for _ in np.arange(10)]})
Run Code Online (Sandbox Code Playgroud)
您可以看到4月和5月的记录丢失,我想将丢失记录的销售额列为零:
sales = insert_zero_for_missing(sales_with_missing)
print(sales)
Run Code Online (Sandbox Code Playgroud)
我该如何实现该insert_zero_for_missing方法?
dataframe ×6
pandas ×6
python ×5
r ×3
ggplot2 ×1
group-by ×1
javascript ×1
jestjs ×1
lambda ×1
numpy ×1
python-2.7 ×1
reactjs ×1
regex ×1
statistics ×1
sum ×1
time-series ×1