首页 > 解决方案 > 使用Python为Excel中多列的条件创建不同的数据框

问题描述

我想为Main Date > Reported DateNumber(Column B)的那些创建一个不同的数据框(见下图)。如果这个条件成真,那么我必须让其他数据框显示该数字数据。

例子

:- if take Number (column B) 223311, now if any main date > Reported Date, then display all the records of that Number

在此处输入图像描述

标签: pythonsqlpandas

解决方案


这是一个简单的 Pandas 解决方案。您可以通过特定列的列值非常容易地分离出数据框。从那里,迭代新的数据框,重置索引(如果要保留索引,请改用 dataframe.shape)。为了方便起见,我将它们附加到一个列表中,可以很容易地将其提取到带标签的数据帧中,或者组合起来。长变量名有助于理解。

df = pd.read_csv('forstack.csv')
list_of_dataframes = [] #A place to store each dataframe. You could also name them as you go
checked_Numbers = [] #Simply to avoid multiple of same dataframe

for aNumber in df['Number']: #For every number in the column "Number"
    if(aNumber not in checked_Numbers): #While this number has not been processed
        checked_Numbers.append(aNumber) #Mark as checked
        df_forThisNumber = df[df.Number == aNumber].reset_index(drop=True) #"Make a different Dataframe" Per request, with new index
        for index in range(0,len(df_forThisNumber)): #Parse each element of this dataframe to see if it matches criteria
            if(df_forThisNumber.at[index,'Main Date'] > df_forThisNumber.at[index,'Reported Date']):
                list_of_dataframes.append(df_forThisNumber) #If it matches the criteria, append it

输出:

  Main Date  Number Reported Date  Fee  Amount  Cost  Name
0  1/1/2019  223311      1/1/2019  100      12    20    11
1  1/7/2019  223311      1/1/2019  100      12    20    11
  Main Date  Number Reported Date  Fee  Amount  Cost  Name
0  1/2/2019  111111      1/2/2019  100      12    20    11
1  1/6/2019  111111      1/2/2019  100      12    20    11
  Main Date  Number Reported Date  Fee  Amount  Cost  Name
0  1/3/2019  222222      1/3/2019  100      12    20    11
1  1/8/2019  222222      1/3/2019  100      12    20    11

推荐阅读