pandas数据的读取操作方法汇总分析(涉及转义字符的数据地址问题以泰坦尼克号数据为例)

Python_报错:SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape - 翻滚的小强 - 博客园 (cnblogs.com)https://www.cnblogs.com/rychh/p/9743864.html经常会遇到数据的读取,数据的读取是数据处理使用pandas数据读取汇总,以泰坦尼克号的数据为样本,具体见方法.

方法一:

import os
import urllib.request
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras import layers
from sklearn import preprocessing
import matplotlib.pyplot as plt

 网页直接下载获取:

#下载的网页路径
url='http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic3.xls'
filepath="titanic3.xls" #文件路径
if not os.path.isfile(filepath):
    result = urllib.request.urlretrieve(url,filepath) #该函数可以将远程数据下载到本地
    print(result)

方法二:在数据已经在离线电脑里了如在绝对地址filepath="C:\Users\Victor\Titanic\train.csv",当前地址里直接有 train.csv,使用当前地址方法:

train = pd.read_csv("train.csv",header=0) # one way to read
print(train.describe())

方法三:使用绝对地址方法:

f=open(r'C:\Users\Victor\Titanic\train.csv')#加上r,声明字符串,不用转义处理
df=pd.read_csv(f)

方法四:直接绝对地址使用

filepath="C:\\Users\\Victor\\Titanic\\train.csv"
all_df = pd.read_csv(filepath)

所有方法的结果如下图:

对于读取离线地址的绝对地址使用主要有三种方法:

一:更换为绝对路径的写法
func1("C:\\Users\\renyc")

二:显式声明字符串不用转义(加r)
func1(r"C:\Users\renyc")

三:使用Linux的路径/
func1("C:/Users/renyc")

参考链接:Pandas数据分析①——数据读取(CSV/TXT/JSON)_cindy407的博客-CSDN博客_pandas读取csvhttps://blog.csdn.net/cindy407/article/details/90747049?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2~default~OPENSEARCH~default-1.highlightwordscore&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2~default~OPENSEARCH~default-1.highlightwordscore Python_报错:SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape - 翻滚的小强 - 博客园 (cnblogs.com)https://www.cnblogs.com/rychh/p/9743864.html

pandas.get_dummies 的用法_数据分析-CSDN博客_pandas.get_dummiesicon-default.png?t=LA92https://blog.csdn.net/maymay_/article/details/80198468

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值