安装pandas
pip install --upgrade pandas -i https://pypi.tuna.tsinghua.edu.cn/simple/
读取csv文件
1. 读取文件夹中的所有csv文件
import glob,os
import pandas as pd
path = r'D:\test'
file = glob.glob(os.path.join(path,'*csv'))
dl = []
for f in file:
dl.append(pd.read_csv(f))
data = pd.concat(dl)
2. 读取单个文件
import pandas as pd
import os
path = r'D:\test'
file = r'test.csv'
file_path = os.path.join(path,file)
data = pd.read_csv(file_path)
整列数据移动
1. 将原来一列向下移动一行成为新的一列
import pandas as pd
data = {"name":['BMW','Benz','Audi'],"sales":[100,200,300],"price":[1,2,3]}
df = pd.DataFrame(data)
df.head()
name sales price
0 BMW 100 1
1 Benz 200 2
2 Audi 300 3
df['new_price'] = dl['price'].shift(1)
df.head()
name sales price new_price
0 BMW 100 1 NaN
1 Benz 200 2 1.0
2 Audi 300 3 2.0
2.同理,向上移动
df['new_price2'] = dl['price'].shift(-1)
df.head()
name sales price new_price new_price2
0 BMW 100 1 NaN 2.0
1 Benz 200 2 1.0 3.0
2 Audi 300 3 2.0 NaN