python_使用

来源:互联网 发布:数据库原理及应用教程 编辑:程序博客网 时间:2024/04/29 11:13

1、系统相关

  • !cmd Execute cmd in the system shell
  • output = !cmd args Run cmd and store the stdout in output
  • %alias alias_name cmd Define an alias for a system (shell) command
  • %bookmark Utilize IPython’s directory bookmarking system
  • %cd directory Change system working directory to passed directory
  • %pwd Return the current system working directory
  • %pushd directory Place current directory on stack and change to target directory
  • %popd Change to directory popped off the top of the stack
  • %dirs Return a list containing the current directory stack
  • %dhist Print the history of visited directories
  • %env Return the system environment variables as a dict

2、ipython定义类

class Message:
def __init__(self, msg):
self.msg = msg
def __repr__(self):
return 'Message: %s' % self.msg

3、类型转换

(1)rr.astype(np.int32)//类型转换

(2)string 转float

numeric_strings = np.array(['1.25', '-9.6', '42'], dtype=np.string_)
numeric_strings.astype(float)
array([ 1.25, -9.6 , 42. ])

4、array 并不copy

 (1) arr_slice = arr[5:8] //相当于引用

arr_slice[1] = 12345
Out[59]: array([ 0, 1, 2, 3, 4, 12, 12345, 12, 8, 9])
In [60]: arr_slice[:] = 64
Out[61]: array([ 0, 1, 2, 3, 4, 64, 64, 64, 8, 9])

(2)拷贝:arr[5:8].copy()

5、numpy 保存数据

(1)      numpy.save('test', var)

x = numpy.load('test.npy')

(2) 保存多个数据

numpy.savez('zdad.npz', a= arr, b= arr)

x = numpy.load('zdad.npz')

x['a'], x['b']

6、numpy 读取文件按

numpy.genfromtxt('world_alcohol.csv', delimiter = ',',dtype = 'U75', skip_header = True)、、delimiter 利用 ‘,’进行分割,u75:设置成unicode自字符,skip_header:跳过表的抬头
print (world_alcohol)


7、numpy

numpy.sum(aix=1), 每行求和

numpy.sum(aix=0), 每列求和


8、pandas 获取以 ‘g‘结尾的列

print(food_info.columns)
print(food_info.head(2))
col_names = food_info.columns.tolist()//获取表的列名
gram_columns = []


for c in col_names:
    if c.endswith("(g)"):
        gram_columns.append(c)
gram_df = food_info[gram_columns]
print(gram_df.head(3))

0 0
原创粉丝点击