Ubuntu 下的 Python 数据分析实战指南
一 环境搭建与工具选型
sudo apt update && sudo apt install -y python3 python3-pippython3 -m venv venv && source venv/bin/activatebash Anaconda3-<版本>-Linux-x86_64.sh,随后 source ~/.bashrcpip install pandas numpy matplotlib seaborn scikit-learn jupyterconda install pandas numpy matplotlib seaborn scikit-learn jupyter二 端到端实战流程
df = pd.read_csv('data.csv')engine = create_engine('mysql+pymysql://user:pwd@host:3306/db'); df = pd.read_sql('SELECT * FROM t', engine)requests + BeautifulSoup 获取并解析 HTMLdf.dropna() 或 df['col'].fillna(df['col'].mean(), inplace=True)df.drop_duplicates(inplace=True)df['date'] = pd.to_datetime(df['date'])df.describe(include='all')sns.histplot, sns.scatterplot, sns.boxplotdf.groupby('cat').agg({'val':'mean'})pd.pivot_table(values='val', index='row', columns='cat')train_test_split + LinearRegression + mean_squared_error/r2_scoreplt.savefig('chart.png', dpi=150, bbox_inches='tight')reportlab 生成 PDF 报告三 远程与服务器场景
jupyter notebook --generate-configc.NotebookApp.ip = '0.0.0.0'c.NotebookApp.open_browser = Falsec.NotebookApp.port = 8888jupyter notebook(如以 root 运行:jupyter notebook --allow-root)http://<服务器IP>:8888,使用终端输出的 token 登录四 性能与扩展建议
pip install "dask[complete]",以分块计算与延迟求值提升可扩展性pip install 相关 Python 包五 常见问题与快速排障
python3 与 pip3;在虚拟环境中直接用 python、pipsource venv/bin/activate,并用 which python 检查解释器路径c.NotebookApp.ip='0.0.0.0'、端口 8888 已放行;如使用云服务器,检查安全组/防火墙规则fonts-noto-cjk),并在 Matplotlib 中设置字体后再绘图与保存pip list --outdated 与 pip check 排查冲突包版本