Reputation: 1043
I am very new to IPython, but not new to py itself. I am going through some code examples from a book called datadrivensecurity and trying to run one of the code examples. When i create a new file in IPython (using cannopy), then click run, i get the following output in the console window.
In [9]: %run /Users/myuser/Documents/Notebooks/ch02.py
highvulns int64
name object
os object
dtype: object
In [10]:
When i copy/paste the code into the In[#]
console prompt, i get the output expected. What am i doing wrong ?
#
# name ch02.py
#
# create a new data frame
import numpy as np
import pandas as pd
# create a new data frame of hosts & high vuln counts
assets_df = pd.DataFrame( {
"name" : ["danube","gander","ganges","mekong","orinoco" ],
"os" : [ "W2K8","RHEL5","W2K8","RHEL5","RHEL5" ],
"highvulns" : [ 1,0,2,0,0 ]
} )
# take a look at the data frame structure & contents
print(assets_df.dtypes)
assets_df.head()
# show a "slice" just the operating systmes
assets_df.os.head()
# add a new column
assets_df['ip'] = [ "192.168.1.5","10.2.7.5","192.168.1.7",
"10.2.7.6", "10.2.7.7" ]
# show only nodes with more than one high vulnerabilty
assets_df[assets_df.highvulns>1].head()
# divide nodes into network 'zones' based on IP address
assets_df['zones'] = np.where(
assets_df.ip.str.startswith("192"), "Zone1", "Zone2")
# get one final view
assets_df.head()
highvulns int64
name object
os object
dtype: object
Out[7]:
highvulns
name
os
ip
zones
0
1
danube
W2K8
192.168.1.5
Zone1
1
0
gander
RHEL5
10.2.7.5
Zone2
2
2
ganges
W2K8
192.168.1.7
Zone1
3
0
mekong
RHEL5
10.2.7.6
Zone2
4
0
orinoco
RHEL5
10.2.7.7
Zone2
Upvotes: 0
Views: 221
Reputation: 5810
As a convenience, if you type an expression at the prompt, the value of the expression will be printed. But if you just write the same expression in a python file, it will be evaluated, but the value will not be printed. You should print x
if you want the value of x to be printed from a file that you are running.
Upvotes: 1