pandas IO

复制代码
pd.read_csv("../data/user_info.csv", index_col="name") #假设csv里包含这几列: name, age, birth, sex
data="name,age,birth,sex\nTom,18.0,2000-02-10,\nBob,30.0,1988-10-17,male"
print(data)
pd.read_csv(StringIO(data))#从 StringIO 对象中读取。
data = "name|age|birth|sex~Tom|18.0|2000-02-10|~Bob|30.0|1988-10-17|male"
pd.read_csv(StringIO(data), sep="|", lineterminator="~")  #自定义字段之间的分隔符
pd.read_csv(StringIO(data), sep="|", lineterminator="~", dtype={"age": int}) # 自己指定数据类型
data="Tom,18.0,2000-02-10,\nBob,30.0,1988-10-17,male"
pd.read_csv(StringIO(data), names=["name", "age", "birth", "sex"])  csv文件并没有标题,我们可以设置参数 names 来添加标题。
pd.read_csv(StringIO(data), usecols=["name", "age"]) # 只读取部分列
print(user_info.to_json()) #将dataframe转成json字符串
复制代码

 

格式类型数据描述ReaderWriter
text CSV read_csv to_csv
text JSON read_json to_json
text HTML read_html to_html
text clipboard read_clipboard to_clipboard
binary Excel read_excel to_excel
binary HDF5 read_hdf to_hdf
binary Feather read_feather to_feather
binary Msgpack read_msgpack to_msgpack
binary Stata read_stata to_stata
binary SAS read_sas  
binary Python Pickle read_pickle to_pickle
SQL SQL read_sql to_sql
SQL Google Big Query read_gbq to_gbq

 

 

 to_json
split 字典像索引 - > [索引],列 - > [列],数据 - > [值]}
records 列表像{[列 - >值},…,{列 - >值}]
index 字典像{索引 - > {列 - >值}}
columns 字典像{列 - > {索引 - >值}}
values 只是值数组

 

 

 

posted on   我和你并没有不同  阅读(516)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示