我有一个数据框,其中记录了19717人通过多项选择题对编程语言的选择的回答。第一栏当然是受访者的性别,其余则是他们选择的选项。因此,如果我选择Python,则我的响应将记录在Python列中,而不是bash,反之亦然。
ID Gender Python Bash R JavaScript C++
0 Male Python nan nan JavaScript nan
1 Female nan nan R JavaScript C++
2 Prefer not to say Python Bash nan nan nan
3 Male nan nan nan nan nan
Run Code Online (Sandbox Code Playgroud)
我想要的是一个表,该表返回Gender
记录下每个类别的实例数。因此,如果用Python用Python编码的5000名男性和用JS编码的女性3000名,那么我应该得到:
Gender Python Bash R JavaScript C++
Male 5000 1000 800 1500 1000
Female 4000 500 1500 3000 800
Prefer Not To Say 2000 ... ... ... 860
Run Code Online (Sandbox Code Playgroud)
我尝试了一些选项:
df.iloc[:, [*range(0, 13)]].stack().value_counts()
Male 16138
Python 12841
SQL 6532
R …
Run Code Online (Sandbox Code Playgroud) 较新版本的 Conda 为用户提供了有关导致与环境冲突的不一致包的更多详细信息。我的 conda env 中的一个这样的包是_nb_ext_conf
根据 anaconda cloud,它是我系统中安装的最新 0.4.0,即使如此,我在尝试安装/更新模块时也会收到此警告:
The environment is inconsistent, please check the package plan carefully
The following packages are causing inconsistency:
- defaults/osx-64::_nb_ext_conf==0.4.0=py36_1
Run Code Online (Sandbox Code Playgroud)
无法删除此包,因为 jupyterlab 和 notebook 都需要它。我不确定它是否会导致严重的冲突,因为我还没有遇到任何损坏的情况,但我仍然想解决这种不一致的问题。有什么办法可以修复它吗?我在 github 的 conda 问题部分搜索了类似的问题,但没有遇到任何类似的问题。
要将某些数字打印为二进制格式,我们只需使用.format()
方法,如下所示:
# Binary
for i in range(5+1):
print("{0:>2} in binary is {0:>08b}".format(i))
0 in binary is 00000000
1 in binary is 00000001
2 in binary is 00000010
3 in binary is 00000011
4 in binary is 00000100
5 in binary is 00000101
Run Code Online (Sandbox Code Playgroud)
对于其他格式(十六进制和八进制)的打印也是如此,只需要将后者的花括号替换为我们要打印的数字即可。但是,有没有办法使用新f""
字符串替换.format()
命令?我知道这看似微不足道,但我在试用新功能时不胜其烦,此外还f""
使代码更短,更易读。
for i in range(5+1):
print(f'{0:>2} in binary is {0:>08b}')
# This prints out just 0s
Run Code Online (Sandbox Code Playgroud) 所以我最近才开始发现yellowbrick
图书馆的力量(谢谢!)但是有没有办法为内联图设置图形大小?
我有一个简单的数字字典及其各自的频率,我结合了两个列表:
elements = [1, 2, 3, 4, 5]
frequency = [5, 4, 3, 2, 1]
combined = {ele: freq for ele, freq in zip(elements, frequency)}
>>> print(combined)
{1: 5, 2: 4, 3: 3, 4: 2, 5: 1}
Run Code Online (Sandbox Code Playgroud)
现在我希望每个ele
都被它重复freq
并保存在一个新列表中。理想情况下,最好是repeat()
从 numpy使用。
repeated = list(np.repeat(key, val) for key, val in sorted(combined.items())) # If a dict needs sorting
>>> print(repeated)
[1,1,1,1,1,2,2,2,2, ... ,4,4,5]
Run Code Online (Sandbox Code Playgroud)
但是,我想在不使用重复的情况下做到这一点。什么是进行的好方法?
我有一个几个整数值的映射,我想迭代它并打印它的值.我试过这个:
n = map(int, input().split())
1 2 3 4 5
for i in n:
print(i)
Run Code Online (Sandbox Code Playgroud)
这给了我一个错误:
ValueError: invalid literal for int() with base 10: ' '
Run Code Online (Sandbox Code Playgroud)
使用上面的相同操作.strip()
执行打印整数的工作.
del(n)
n = map(int, input().strip().split())
1 2 3 4 5
for i in n:
print(i)
1
2
3
4
5
Run Code Online (Sandbox Code Playgroud)
"基数为10的无效文字"是什么意思以及为什么使用.strip()
修复错误?另外,地图对象是Python中的单个实体,因为使用时range(map)
会给出错误'map'对象不能被解释为整数?
for i in range(n):
print(i)
TypeError: 'map' object cannot be interpreted as an integer
Run Code Online (Sandbox Code Playgroud) python ×6
anaconda ×1
binary ×1
conda ×1
dataframe ×1
dictionary ×1
f-string ×1
package ×1
pandas ×1
python-3.x ×1
virtualenv ×1
yellowbrick ×1