文章来源: 量化小课堂
#!/usr/bin/env python
#coding:utf-8
import os
import pandas as pd
file_list = []
for root, dirs, files in os.walk('data'):
if files:
for f in files:
if '.csv' in f:
file_list.append(f.split('.csv')[0])
output = pd.DataFrame()
for f in file_list:
code = f.split()[-1].strip()
print code
stock_data = pd.read_csv('data' + f + '.csv', parse_dates=[0])
stock_data['Money'] = stock_data['Volume'] * stock_data['Price']
l = len(output)
output.loc[l, 'code'] = code
output.loc[l, '平均每笔交易成交量'] = stock_data['Volume'].mean()
data = stock_data.groupby('BullSell')['Money'].sum()
if 'B' in data.index:
output.loc[l, '资金流入'] = data['B']
if 'S' in data.index:
output.loc[l, '资金流出'] = data['S']
data = stock_data[stock_data['Volume'] > 50000].groupby('BuySell')['Money'].sum()
if 'B' in data.index:
output.loc[l, '主力资金流入'] = data['B']
if 'S' in data.index:
output.loc[l, '主力资金流出'] = data['S']
output.to_csv('资金流数据.csv', index=False, encoding='gbk')