如何根据熊猫中的条件替换列之间的值?

围兜

我有一个表单的数据框

ID              Effective_Date        Paid_Off_Time

xqd27070601     09 August 2016        10 July 2016
xqd21601070     09 September 2016     10 July 2016
xqd26010760     10 July 2016          09 November 2016

编辑最初,显示的日期是字符串类型。它们的格式可以是:像这样9/18/2016 16:56, 09 August 2016, 9/18/2016。我们是否应该考虑转换为时间戳以便于比较?

如果Effective_Date> Paid_Off_Time , replace value of Effective_Date withPaid_Off_Time and the value ofPaid_Off_Time withEffective_Date``我想要什么基本上,在 2 列之间切换值,因为日期插入错误的列中。

我考虑过使用np.where,但我想知道,难道没有更简洁、更简洁的解决方案吗?

#create a new dataFrame
testDf = pd.DataFrame(columns=['Effective_Date','Paid_Off_Time'])

#check if Effective_Date < myDataFrame
testDf['Effective_Date'] = np.where(myDataFrame.Effective_Date < myDataFrame.Paid_Off_Time,myDataFrame.Effective_Date,myDataFrame.Paid_Off_Time)

#check if Paid_Off_Time < Effective_Date
testDf['Paid_Off_Time'] = np.where(myDataFrame.Paid_Off_Time < myDataFrame.Effective_Date,myDataFrame.Effective_Date,myDataFrame.Paid_Off_Time)

myDataFrame['Effective_Date'] = testDf[testDf['Effective_Date']]
myDataFrame['Paid_Off_Time'] = testDf[testDf['Paid_Off_Time']]
墙壁
Convert dates to datetime

df=df.assign(Effective_Date=pd.to_datetime(df['Effective_Date'], format='%d %B %Y'),Paid_Off_Time=pd.to_datetime(df['Paid_Off_Time'], format='%d %B %Y'))

根据条件选择

m=df.Effective_Date>df.Paid_Off_Time

如果条件满足则交换值

 df.loc[m, ['Effective_Date','Paid_Off_Time']]=df.loc[m, ['Paid_Off_Time','Effective_Date']].values#Swap rows if condition met
    print(df)



  ID     Effective_Date Paid_Off_Time
0  xqd27070601     09 August 2016  10 July 2016
1  xqd21601070  09 September 2016  10 July 2016
2  xqd26010760   09 November 2016  10 July 2016

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章