简体   繁体   English

如何在数据框中使用该数据框中另一列的值创建一个新列,但在 1 小时内

[英]How to create a new column in a dataframe with the value of another column in that dataframe, but within 1 hour

I have a pandas dataframe, with columns:我有一个熊猫数据框,列:

  • ID ID
  • DateTime约会时间
  • Capacity容量

I want to add a new column to this dataframe, with the capacity within an hour.我想向此数据框中添加一个新列,容量在一小时内。 For example, I have例如,我有

ID Datetime           Capacity
1  20200101 12:23:10  435

So in the new column, I want the Capacity of the record of ID=1 and Datetime=13:23:10所以在新列中,我想要ID=1和Datetime=13:23:10的记录的容量

Is there a statement to handle this?有没有处理这个的声明?

Thanks already !已经谢谢了!

You can add 1 hour to original column, convert to strings HH:MM:SS , so possible filtering in boolean indexing :您可以将1 hour添加到原始列,转换为字符串HH:MM:SS ,因此可以在boolean indexing进行过滤:

Datetime = '13:23:10'
ID = 1

df['Datetime'] = pd.to_datetime(df['Datetime'])

df = df[(df['Datetime'] + pd.Timedelta('1H')).dt.strftime('%H:%M:%S').eq(Datetime) & df['ID'].eq(ID)]
print (df)
   ID            Datetime  Capacity
0   1 2020-01-01 12:23:10       43

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何基于另一个DataFrame中的列在Pandas DataFrame中创建新列? - How to create a new column in a Pandas DataFrame based on a column in another DataFrame? 根据另一个 dataframe 的值创建新列 dataframe 运行速度快吗? - create new column of dataframe base on value of another dataframe run fast? 创建新的数据框列,保留另一列的第一个值 - Create new dataframe column keeping the first value from another column 如何通过过滤另一个 dataframe 的列来创建新的 dataframe - How to create new dataframe by filtering a column of another dataframe 如何根据另一个 dataframe 中的条件在 dataframe 中创建新列? - how to create a new column in a dataframe based on conditions in another dataframe? 如何在 dataframe 中创建新列,当另一列中的 diff() 小于 0 时添加 1? - How to create new column in a dataframe that add 1 when diff() within another column is less than 0? 如何根据另一个 dataframe 的匹配为 dataframe 的新列添加值? - how to add value to a new column to a dataframe based on the match of another dataframe? 如何创建一个新的数据框列,并从另一个列中移出值? - How to create a new dataframe column with shifted values from another column? 如何通过将另一列乘以常数来在 dataframe 中创建新列? - How to create a new column in a dataframe by multiplying another column by a constant? 如何在 DataFrame 的另一列中创建具有特定值出现百分比的新列? - How to create a new column with the percentage of the occurance of a particular value in another column in a DataFrame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM