数据分析-pandas存储文本文件

import pandas as pd

df = pd.read_csv('datafile/data4.csv')

print(df)
    k1 k2   k3    k4
0  one  a  1.0  11.0
1  one  b  2.0   NaN
2  one  c  3.0  13.0
3  one  d  NaN  14.0
4  one  e  5.0  15.0
5  two  f  6.0   NaN
6  two  g  7.0  17.0
7  two  h  NaN  18.0
8  two  i  9.0  19.0
df = pd.read_csv('datafile/data4.csv')

df.to_csv('datafile/outer4.csv')
# 生成 csv 文件

pd.read_csv('datafile/outer4.csv')
Unnamed: 0k1k2k3k4
00onea1.011.0
11oneb2.0NaN
22onec3.013.0
33onedNaN14.0
44onee5.015.0
55twof6.0NaN
66twog7.017.0
77twohNaN18.0
88twoi9.019.0
df = pd.read_csv('datafile/data4.csv')

df.to_csv('datafile/outer5.csv', na_rep="0")
# na_rep 设置缺失值存储在文件中的值

pd.read_csv('datafile/outer5.csv')
Unnamed: 0k1k2k3k4
00onea1.011.0
11oneb2.00.0
22onec3.013.0
33oned0.014.0
44onee5.015.0
55twof6.00.0
66twog7.017.0
77twoh0.018.0
88twoi9.019.0
df = pd.read_csv('datafile/data4.csv')

df.to_csv('datafile/outer6.csv',index=False, header=False)
# 不写出列名和行名

pd.read_csv('datafile/outer6.csv')
onea1.011.0
0oneb2.0NaN
1onec3.013.0
2onedNaN14.0
3onee5.015.0
4twof6.0NaN
5twog7.017.0
6twohNaN18.0
7twoi9.019.0
df = pd.read_csv('datafile/data4.csv')

df.to_csv('datafile/outer7.csv',index=False, header=False, columns=['k1', 'k2', 'k3', 'k4'])
# columns 指定写入的列

pd.read_csv('datafile/outer7.csv')
onea1.011.0
0oneb2.0NaN
1onec3.013.0
2onedNaN14.0
3onee5.015.0
4twof6.0NaN
5twog7.017.0
6twohNaN18.0
7twoi9.019.0
df = pd.read_csv('datafile/data4.csv')
print(df)
df.to_csv('datafile/outer8.csv', na_rep='0',index=False,header=['A', 'B', 'C', 'D'])
# header 设置列名
# index_label 不会用啊!!!

pd.read_csv('datafile/outer8.csv')
    k1 k2   k3    k4
0  one  a  1.0  11.0
1  one  b  2.0   NaN
2  one  c  3.0  13.0
3  one  d  NaN  14.0
4  one  e  5.0  15.0
5  two  f  6.0   NaN
6  two  g  7.0  17.0
7  two  h  NaN  18.0
8  two  i  9.0  19.0
ABCD
0onea1.011.0
1oneb2.00.0
2onec3.013.0
3oned0.014.0
4onee5.015.0
5twof6.00.0
6twog7.017.0
7twoh0.018.0
8twoi9.019.0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值