python - 使用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
解决方案
这是一个简单的 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
推荐阅读
- javascript - 在 Firebase 实时数据库 (Redux) 中增加一个值
- keyboard - 使用 DirectInput (dinput.dll) 模拟带有鼠标移动的按键
- pine-script - 如果价格变动超过 50%,Pinescript 会发出警报
- pip - 不能通过 --version 和其他包调用 pip 包 diamond 的版本
- angular - 无法检测到 Angular Routes 数组
- python - 如何使用 AWS 复制 Zapier 集成?
- python - Discord BOT,我想给每个人发送一个随机报价,除了一个用户
- amazon-web-services - SQS:AWS.SimpleQueueService.UnsupportedOperation:不支持 SendMessage 操作中的消息属性列表值
- python - AttributeError:“超级”对象在部署时没有属性“get_params”
- spring-boot - Spring Boot 缓存功能不需要 spring-boot-starter-cache