python读写CSV文件
做数据分析,有时候要分析的数据在CSV文件里;先看一下python读写CSV文件;
import pandas as pd
df = pd.read_csv('test1.csv')
print(df)
print('')
print(df.head(2))
companyname = ["A1", "B2", "E3", "F4"]
legperson = ["lier", "yanqi", "wangwu", "zhangsan"]
legmobile = ["13900001111", "13900008888", "13500001111", "13500001234"]
dict = {'companyname': companyname, 'legperson': legperson, 'legmobile': legmobile}
df2 = pd.DataFrame(dict)
df2.to_csv('companys.csv')
首先读test1.csv,该文件如下,
读取以后输出,使用head(n)输出头n行,
然后再定义一些数据,保存为companys.csv,保存后的文件如下,