我有一个 pandas 数据框,我想在 Jupyter 笔记本中完整地打印它(大约 90 行)。如果可能的话,我还想在没有索引列的情况下显示它。我怎样才能做到这一点?
我正在制作几个图,为了灵活性,我计划将其包装在函数中,作为其中的一部分,我想根据某些条件为这些图指定分面。
具体来说,我有一个列表grouping_vars(在其他地方指定),用于对数据执行其他一些操作。我希望我的绘图函数能够facet_wrap在只有一个时自动使用 a grouping_var,或者如果 中指定了两个或多个变量grouping_var,则使用前两个 for facet_grid。
例如,如果基本情节是这样的
mtcars %>% ggplot() + geom_point(aes(x = hp, y = mpg))
Run Code Online (Sandbox Code Playgroud)
如果我指定grouping_vars <- c("cyl"),我实际上想得到,
mtcars %>% ggplot() +
geom_point(aes(x = hp, y = mpg)) +
facet_wrap(~cyl)
Run Code Online (Sandbox Code Playgroud)
而如果我指定grouping_vars <- c("cyl", "carb")我想得到
mtcars %>% ggplot() +
geom_point(aes(x = hp, y = mpg)) +
facet_grid(vars(cyl), vars(carb))
Run Code Online (Sandbox Code Playgroud)
mtcars %>% ggplot() +
geom_point(aes(x = hp, y = mpg)) +
{if(length(grouping_vars==1)) facet_wrap(grouping_vars[[1]])} +
{if(length(grouping_vars>=2)) …Run Code Online (Sandbox Code Playgroud) 我有一个类,其属性基于用户定义的字典初始化(使用 JSON 读取):
class Knight(object):
def __init__(self, traits):
for k, v in traits.items():
self.__setattr__(k, v)
traitfile = json.load(open(input(), 'r'))
# Where the input file is e.g.
# {'helmet': 'horned',
# 'sword': 'big',
# 'words': ['Ni!', 'Peng', 'Neee-Wom!']}
Run Code Online (Sandbox Code Playgroud)
当我实例化对象时,helmet、sword、 和words成为预期的属性。但是,如果我随后更改实例属性,它似乎会影响最初初始化对象的原始字典:
tall_knight = Knight(traitfile)
print(tall_knight.words) # prints ['Ni!', 'Peng', 'Neee-Wom!']
print(traitfile['words']) # also prints ['Ni!', 'Peng', 'Neee-Wom!']
tall_knight.words.append('Ekke ekke!')
print(tall_knight.words) # prints ['Ni!', 'Peng', 'Neee-Wom!', 'Ekke ekke!'] as expected
print(traitfile['words']) # also prints ['Ni!', 'Peng', 'Neee-Wom!', …Run Code Online (Sandbox Code Playgroud)