Python 通过使用循环将字典中的所有数据帧子集化来创建新的数据帧

Python 通过使用循环将字典中的所有数据帧子集化来创建新的数据帧,python,pandas,Python,Pandas,我有一个数据帧字典: two_season_bucket_suffixes = {'two_season_bucket_year1_racer_bio':two_season_bucket_year1_racer_bio, 'two_season_bucket_year1_spring_rate':two_season_bucket_year1_spring_rate, 'two_season_bucket_year1_neaps_rate':two_season_bucket_year1_ne

我有一个数据帧字典:

two_season_bucket_suffixes = {'two_season_bucket_year1_racer_bio':two_season_bucket_year1_racer_bio,
'two_season_bucket_year1_spring_rate':two_season_bucket_year1_spring_rate,
'two_season_bucket_year1_neaps_rate':two_season_bucket_year1_neaps_rate,
'two_season_bucket_year1_spring_raw':two_season_bucket_year1_spring_raw,
'two_season_bucket_year1_neap_raw':two_season_bucket_year1_neap_raw,
'two_season_bucket_year1_opposing_team':two_season_bucket_year1_opposing_team,
'two_season_bucket_year1_opposing_team_distribution':two_season_bucket_year1_opposing_team_distribution,
'two_season_bucket_year1_stern_score':two_season_bucket_year1_stern_score,
'two_season_bucket_year1_bow_score':two_season_bucket_year1_bow_score,
'two_season_bucket_year1_team_score':two_season_bucket_year1_team_score,
'two_season_bucket_year2_racer_bio':two_season_bucket_year2_racer_bio,
'two_season_bucket_year2_spring_rate':two_season_bucket_year2_spring_rate,
'two_season_bucket_year2_neaps_rate':two_season_bucket_year2_neaps_rate,
'two_season_bucket_year2_spring_raw':two_season_bucket_year2_spring_raw,
'two_season_bucket_year2_neap_raw':two_season_bucket_year2_neap_raw,
'two_season_bucket_year2_opposing_team':two_season_bucket_year2_opposing_team,
'two_season_bucket_year2_opposing_team_distribution':two_season_bucket_year2_opposing_team_distribution,
'two_season_bucket_year2_stern_score':two_season_bucket_year2_stern_score,
'two_season_bucket_year2_bow_score':two_season_bucket_year2_bow_score,
'two_season_bucket_year2_team_score':two_season_bucket_year2_team_score}
它们都有不同的列,但都至少有一列以“prediction”一词开头

我想从每个以“预测”开头的列中创建一个新的数据帧:

two_season_bucket_prediction= pd.DataFrame()
counter = 0
for key, val in two_season_bucket_suffixes.items():
    if counter == 0:
        two_season_bucket_prediction= val[val.columns[pd.Series(val.columns).str.startswith('prediction')]]
    else:
        two_season_bucket_prediction= two_season_bucket_prediction.join(val[val.columns[pd.Series(val.columns).str.startswith('prediction')]])
        counter += 1

每次我得到一个数据帧,其中有一列名为“prediction”,即使我确信没有一列只被命名为prediction,你也在增加你从未达到的计数器:

two_season_bucket_prediction= pd.DataFrame()
counter = 0
for key, val in two_season_bucket_suffixes.items():
    if counter == 0:
        two_season_bucket_prediction= val[val.columns[pd.Series(val.columns).str.startswith('prediction')]]
    else:
        two_season_bucket_prediction= two_season_bucket_prediction.join(val[val.columns[pd.Series(val.columns).str.startswith('prediction')]])
    counter += 1

好的,我把计数器移到了与if相同的缩进位置,但是我仍然在新的数据框架中只得到了一列。您的初始数据不够,因此我可以提供帮助。请将您的问题更新到。一个可运行的示例,以便我可以帮助您。