简体   繁体   English

对 Pandas dataframe 中的列的每条记录应用相同的 function

[英]Apply the same function on each record of a column in Pandas dataframe

I have a dataset with a date-time column with a specific format.我有一个具有特定格式的日期时间列的数据集。 I need to create new features out of this column that means I need to add new columns to the dataframe by extracting information from the above-mentioned date-time column.我需要在此列中创建新功能,这意味着我需要通过从上述日期时间列中提取信息来将新列添加到 dataframe。 My sample input dataframe column is like below.我的示例输入 dataframe 列如下所示。

id    datetime         feature2
1    12/3/2020 0:56       1
2    11/25/2020 13:26     0

The expected output is:预期的 output 为:

id    date      hour    mints    feature2
1    12/3/2020   0       56         1
2    11/25/2020  13      26         0

Pandas apply() method may not work for this as new columns are added. Pandas apply() 方法可能不适用于此,因为添加了新列。 What is the best way to do this?做这个的最好方式是什么?

Is there any way which I can apply a single function on each record of the column to do this by applying on the whole column?有什么方法可以在列的每条记录上应用单个 function 来通过在整个列上应用来做到这一点?

pandas series .dt accessor pandas 系列.dt存取器

  • Your datetime data is coming from a pandas column (series), so use the .dt accessor您的日期时间数据来自 pandas 列(系列),因此请使用.dt 访问器
import pandas as pd

df = pd.DataFrame({'id': [1, 2],
                   'datetime': ['12/3/2020 0:56', '11/25/2020 13:26'],
                   'feature2': [1, 0]})
df['datetime'] = pd.to_datetime(df['datetime'])

 id            datetime  feature2
  1 2020-12-03 00:56:00         1
  2 2020-11-25 13:26:00         0

# create columns
df['hour'] = df['datetime'].dt.hour
df['min'] = df['datetime'].dt.minute
df['date'] = df['datetime'].dt.date

# final
 id            datetime  feature2  hour  min        date
  1 2020-12-03 00:56:00         1     0   56  2020-12-03
  2 2020-11-25 13:26:00         0    13   26  2020-11-25

IICU重症监护病房

df.date=pd.to_datetime(df.date)
df.set_index(df.date, inplace=True)
df['hour']=df.index.hour
df['mints']=df.index.minute

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM