import pandas as pd
import tushare as ts
import matplotlib.pyplot as plt
pro = ts.pro_api('ca0cd5409f62a54869ffa4ad1f81f99e0a3d649f4e87cfc034dc0003')
code_sheet1 = pro.stock_basic(exchange='SSE')
code_sheet2 = pro.stock_basic(exchange='SZSE')
code_sheet = pd.concat([code_sheet1, code_sheet2])
code_bj = code_sheet[code_sheet['industry'] == '电气设备']
print(code_bj)
df1 = pd.DataFrame()
df2 = pd.DataFrame()
df3 = pd.DataFrame()
**for i in range(len(code_bj)):
df1 = df1.__append_(pro.cashflow(ts_code=code_bj.iloc[i]['ts_code'], period='20211231',fields='ts_code,n_cashflow_act,stot_out_inv_act,c_fr_sale_sg,c_cash_equ_end_period,end_date'),ignore_index=True)
df1 = df1.drop_duplicates(ignore_index=True)
for j in range(len(code_bj)):
df2 = df2.__append_(pro.income(ts_code=code_bj.iloc[j]['ts_code'], period='20211231', fields='ts_code,n_income,revenue,end_date'),ignore_index=True)
df2 = df2.drop_duplicates(ignore_index=True)
for z in range(len(code_bj)):
df3 = df3.__append_(pro.balancesheet(ts_code=code_bj.iloc[z]['ts_code'], period='20211231',fields='ts_code,st_borr,lt_borr,bond_payable,end_date'), ignore_index=True)
df3 = df3.drop_duplicates(ignore_index=True)**
df = pd.concat([df1, df2[['n_income', 'revenue']]], axis=1)
df = pd.concat([df, df3[['st_borr', 'lt_borr', 'bond_payable']]], axis=1)
df['name'] = code_bj['name'].tolist()
df['ratio1'] = round(df['n_cashflow_act'] / df['n_income'], 2)
df['ratio2'] = round(df['c_fr_sale_sg'] / df['revenue'], 2)
df['自由现金流'] = df['n_cashflow_act'] - df['stot_out_inv_act']
df.replace(to_replace=[None], value=0, inplace=True)
plt.rcParams['axes.unicode_minus'] = False
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.figure(figsize=(20, 8))
plt.bar(df['name'], df['ratio1'], label='指标1:经营活动现金流量净额/净利润')
plt.legend(loc='upper left')
plt.xticks(df['name'], rotation=45)
plt.show()
plt.figure(figsize=(20, 8))
plt.bar(df['name'], df['ratio2'], label='指标2:销售商品、提供劳务收到的现金/营业收入')
plt.legend(loc='upper left')
plt.xticks(df['name'], rotation=45)
plt.show()
plt.figure(figsize=(20, 8))
plt.bar(df['name'], df['自由现金流'], label='指标3:自由现金流')
plt.legend(loc='upper left')
plt.xticks(df['name'], rotation=45)
plt.show()
D:\Python_study\4.py:20: FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries is deprecated. In a future version, this will no longer exclude empty or all-NA columns when determining the result dtypes. To retain the old behavior, exclude the relevant entries before the concat operation.
df1 = df1._append(pro.cashflow(ts_code=code_bj.iloc[i]['ts_code'], period='20211231',fields='ts_code,n_cashflow_act,stot_out_inv_act,c_fr_sale_sg,c_cash_equ_end_period,end_date'),ignore_index=True)
File "D:\Python_study\4.py", line 20, in <module>
df1 = df1._append(pro.cashflow(ts_code=code_bj.iloc[i]['ts_code'], period='20211231',fields='ts_code,n_cashflow_act,stot_out_inv_act,c_fr_sale_sg,c_cash_equ_end_period,end_date'),ignore_index=True)