subaochen / subaochen.github.io

MIT License
1 stars 3 forks source link

A股年报数据提取小工具 #25

Open subaochen opened 5 years ago

subaochen commented 5 years ago

https://subaochen.github.io/python/2019/07/11/stock-report-data-miner/

zz2liu commented 5 years ago

df.to_csv(..., index=False) 不写表头数据。Python 可以更简洁,试一下下面的代码。Just for fun!

codes = ['603730', '603757']
years = ['2018-12-31', '2017-12-31']
def each_record(codes, years, row='营业收入(万元)'):
    for code in codes:
        filename = f'data/lrb_{code}.csv'
        df = pd.read_csv(filename, index_col=0)
        x = df.loc[row, years]
        yield [code] + list(x)
summary = pd.DataFrame.from_records(each_record(codes, years), columns=['code'] + years)
summary.to_csv('tmp.csv', index=False)
subaochen commented 5 years ago

@zz2liu df.to_csv(..., index=False) 不写表头数据。Python 可以更简洁,试一下下面的代码。Just for fun!

codes = ['603730', '603757']
years = ['2018-12-31', '2017-12-31']
def each_record(codes, years, row='营业收入(万元)'):
    for code in codes:
        filename = f'data/lrb_{code}.csv'
        df = pd.read_csv(filename, index_col=0)
        x = df.loc[row, years]
        yield [code] + list(x)
summary = pd.DataFrame.from_records(each_record(codes, years), columns=['code'] + years)
summary.to_csv('tmp.csv', index=False)

非常感谢老同学的指点!我尝试了你的代码,不但解决了pandas导出excel/csv的表头问题,还顺便消除了表头数据的硬编码问题!

晚上找个时间再整理消化一下,比如DataFrame.from_record和yield的用法我也不熟悉,需要多多向你学习!