我正在使用os.path.exists()检查文件是否存在,如果它存在,我想显示表,table包含使用数据帧从csv文件收集的信息,但是当我写make_table()时在if else语句中,它没有显示其内容,但每当我在外面写它时,它会向我显示数据。这不应该是这种情况,因为我想在文件存在时显示数据。请查看下面的代码
import pandas
from ipy_table import *
...
save_path = select_season(season)
name_of_file =select_league(league)
filename = save_path + name_of_file +".csv"
new_df_home = df[df.FTR == 'H']
new_df_away = df[df.FTR == 'A']
new_df_draw = df[df.FTR == 'D']
if os.path.exists(filename):
df = pd.read_csv(os.path.join(save_path, name_of_file+".csv"))
Matches = [
['Matches Played', 'Home Wins', 'Draws', 'Away Wins','Full time Home goals', 'Full time Away goals',
'Half time Home goals', 'Half time Away goals'],
[df['Div'].count(), new_df_home['Div'].count(), new_df_draw['Div'].count(), new_df_away['Div'].count(),df['FTHG'].sum(),
df['FTAG'].sum(),df['HTHG'].sum(),df['HTAG'].sum()]];
make_table(Matches)
else:
print("This record is not present")
答案 0 :(得分:0)
可能需要存储make_table(匹配)的结果:
table = make_table(Matches)
用以下代码结束你的代码:
...
else:
print("This record is not present")
table
以便表格在最后打印...