我有一个3D numpy数组看起来像这样
shape(3,1000,100)
[[[2,3,0,2,6,...,0,-1,-1,-1,-1,-1],
[1,4,6,1,4,5,3,...,1,2,6,-1,-1],
[7,4,6,3,1,0,1,...,2,0,8,-1,-1],
...
[8,7,6,4,...,2,4,5,2,1,-1]],
...,
[1,5,6,7,...,0,0,0,0,1]]]
Run Code Online (Sandbox Code Playgroud)
每个数组的通道以0或多个(小于70我确定)-1结束.
现在,我想在每个通道的-1之前只选择30个值,以形成原始numpy数组的子集,其形状为(3,1000,30)
应该是这样的,
[[[...,0],
[...,1,2,6],
[...,2,0,8],
...
[...,2,4,5,2,1]],
...,
[...,0,0,0,0,1]]]
Run Code Online (Sandbox Code Playgroud)
有可能用一些numpy功能吗?希望没有for循环:)
我知道我们可以使用str.contains方法来选择部分字符串。
我的专栏是这样的
col1
V2648
V9174.
V9071
V0021;+
V7615***
()()
random
words
Run Code Online (Sandbox Code Playgroud)
我想选择包含带有 的模式的所有行V+ 4 digits number。因此我们需要对这些字符串应用多个条件。
我的输出会是这样的,
col1
V2648
V9174.
V9071
V0021;+
V7615***
Run Code Online (Sandbox Code Playgroud) 我有一个类似的数据框,
cat_A cat_B cat_C cat_D dog_A dog_B dog_C dog_D
3 2 4 1 9 8 10 6
...
...
Run Code Online (Sandbox Code Playgroud)
我知道如何使用列名来计算列之间的距离,例如
df['ratio_A'] = df['cat_A']/df['dog_A']
cat_A cat_B cat_C cat_D dog_A dog_B dog_C dog_D ratio_A
3 2 4 1 9 8 10 6 3/9
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试通过计算每个列来生成多个列时,还有其他更简便的方法来计算所有列并一次附加新列吗?代替
df['ratio_B'] = df['cat_B']/df['dog_B']
df['ratio_C'] = df['cat_C']/df['dog_C']
df['ratio_D'] = df['cat_D']/df['dog_D']
当列的长度变得很大时,将要复制和粘贴很多冗长的代码。我需要创建2个列表吗?
l1 = [cat_A, cat_B, cat_C, cat_D], l2= [dog_A, dog_B, dog_C, dog_D]
然后使用for循环来实现?
我有一个像这样的数据框,
category target
A 1
B 1
A 0
A 0
A 0
B 0
C 1
C 1
Run Code Online (Sandbox Code Playgroud)
我想计算每个类别中每个目标值的百分比。例如,百分比为
'A'==1 是count(1)/(count(1)+count(0)),
'A'==0 是count(0)/(count(1)+count(0))
我怎样才能得到这样的桌子,
category 1 0
A 25% 75%
B 50% 50%
C 100% 0%
Run Code Online (Sandbox Code Playgroud)
也许我应该使用一些分组功能?
我有一个这样的文本数据框,
id text
1 Thanks. I appreciate your help. I really like this chat service as it is very convenient. I hope you have a wonderful day! thanks!
2 Got it. Thanks for the help; good nite.
Run Code Online (Sandbox Code Playgroud)
我想拆分这些文本句子并将它们与每个 id 匹配。我的预期输出是,
id text
1 Thanks.
1 I appreciate your help.
1 I really like this chat service as it is very convenient.
1 I hope you have a wonderful day!
1 thanks!
2 Got it.
2 Thanks for the help;
2 …Run Code Online (Sandbox Code Playgroud) I tried to split words then count them by using python pandas.
The original data is like,
col_A
happy, not happy
sad,happy
sad, happy
angry, happy
angry, sad
Run Code Online (Sandbox Code Playgroud)
I tried using this function to count the words in col_A.
word_list= df.col_A.apply(lambda x: pd.value_counts(x.split(","))).sum(axis=0)
word_list.sort_values(ascending = False)
Run Code Online (Sandbox Code Playgroud)
It will give me the results like,
angry 2
happy 2
sad 2
happy 2
not happy 1
sad 1
Run Code Online (Sandbox Code Playgroud)
How can I avoid these blanks to return the real counts of values?
I …