Python

python pandas DataFrame笔记

python pandas DataFrame 笔记 import os import numpy as np import pandas as pd 创建DataFrame nbadata = {"name": ["Kobe", "Jordan", "Iverson", "T-mac", "O'Neal", "Duncan", "Manu"], "team": ["Lakers", "Bulls", "76ers", "Rockets", "Lakers", "Spurs", "Spurs"], "number": [8,23,3,1,34,21,20]} nbadf1 = pd.DataFrame(nbadata, columns=['name', 'number']) print(nbadf1, "\n") nbadf2 = pd.DataFrame(np.arange(9).reshape((3,3)), index = ['one', 'two', 'three'], columns = ['pts','rebs','asts']) print(nbadf2, "\n") nbadf = pd.DataFrame(nbadata) print(nbadf.values, "\n") print(nbadf.columns, "\n") name number 0 Kobe 8 1 Jordan 23 2 Iverson 3 3 T-mac 1 4 O'Neal 34 5 Duncan 21 6 Manu 20 pts rebs asts one 0 1 2 two 3 4 5 three 6 7 8 [['Kobe' 8 'Lakers'] ['Jordan' 23 'Bulls'] ['Iverson' 3 '76ers'] ['T-mac' 1 'Rockets'] ["O'Neal" 34 'Lakers'] ['Duncan' 21 'Spurs'] ['Manu' 20 'Spurs']] Index(['name', 'number', 'team'], dtype='object') pandas的DataFrame列的选取 # 选取DataFrame的列 print(nbadf.name, "\n") print(nbadf["name"], "\n") 0 Kobe 1 Jordan 2 Iverson 3 T-mac 4 O'Neal 5 Duncan 6 Manu Name: name, dtype: object 0 Kobe 1 Jordan 2 Iverson 3 T-mac

rmarkdown中使用python

目标 在Rstudio的rmarkdown中调用python并执行python代码。 使用方法 全局 ```{r setup} library(knitr) opts_chunk$set(engine.path = '/usr/bin/python3') ``` 局部chunk ```{python} import sys print(sys.version) ``` 指定python版本 ```{python, engine.path = '/usr/bin/python3'} import sys print(sys.version) ``` 参考链接 https://stackoverflow.com/questions/39069158/how-can-i-specify-rmarkdown-to-use-python3-instead-python-2 https://www.jianshu.com/p/05dc80e104c3 http://rpubs.com/badbye/python-knitr