简体   繁体   English

是否将函数应用于pandas数据框的每一列而没有for循环?

[英]Apply function to every column of pandas dataframe without for loop?

I would like to convert a dataframe of timedeltas into hours. 我想将timedeltas的数据帧转换为小时。 I can do this for one series (one column of the dataframe) but I would like to find a way to apply it to all columns. 我可以针对一个系列(数据框的一列)执行此操作,但我想找到一种将其应用于所有列的方法。

A for loop works, but is there a faster or more pythonic way to do this? for loop可以工作,但是有没有更快或更Python的方法来做到这一点?

import pandas as pd 
import datetime 
import numpy as np


    df = pd.DataFrame({'a': pd.to_timedelta(['0 days 00:00:08','0 days 05:05:00', '0 days 01:01:57']), 
'b' : pd.to_timedelta(['0 days 00:44:00','0 days 00:15:00','0 days 01:02:00']), 
'c': pd.to_timedelta(['0 days 00:34:33','0 days 04:04:00','0 days 01:31:58'])})

df

    a           b           c
0   00:00:08    00:44:00    00:34:33
1   05:05:00    00:15:00    04:04:00
2   01:01:57    01:02:00    01:31:58

for c in df.columns:
    df[c] = (df[c]/np.timedelta64(1,'h')).astype(float)

df

    a           b           c
0   0.002222    0.733333    0.575833
1   5.083333    0.250000    4.066667
2   1.032500    1.033333    1.532778

I've tried using lambda, but there's something I'm getting wrong: 我已经尝试过使用lambda,但是出现了一些问题:

df = df.apply(lambda x: x/np.timedeltat(1, 'h')).astype(float)

Returns the error: 返回错误:

AttributeError: ("'module' object has no attribute 'timedelta'", u'occurred at index a')

Use np.timedelta64 working with all columns converted to 2d numpy array: 使用np.timedelta64处理所有转换为2d numpy数组的列:

df = pd.DataFrame(df.values / np.timedelta64(1, 'h'), columns=df.columns, index=df.index)
print (df)
          a         b         c
0  0.002222  0.733333  0.575833
1  5.083333  0.250000  4.066667
2  1.032500  1.033333  1.532778

If want use apply : 如果要使用,请apply

df = df.apply(lambda x: x/np.timedelta64(1, 'h'))
print (df)
          a         b         c
0  0.002222  0.733333  0.575833
1  5.083333  0.250000  4.066667
2  1.032500  1.033333  1.532778

Or total_seconds : total_seconds

df = df.apply(lambda x: x.dt.total_seconds() / 3600)
print (df)
          a         b         c
0  0.002222  0.733333  0.575833
1  5.083333  0.250000  4.066667
2  1.032500  1.033333  1.532778

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

相关问题 将函数应用于pandas中数据框的每一列 - Apply a function to every column of a dataframe in pandas 将函数应用于pandas数据框列中每一行的每个单词 - apply function to each word of every row in pandas dataframe column 如何将自定义函数应用于pandas中数据框的每一列? - How to apply a custom function to every column of a dataframe in pandas? 如何将函数应用于pandas数据框中列的每个值? - How to apply a function to every value in a column in a pandas dataframe? apply() 应用于 ExcelFile、Pandas 的每个数据框的每一列 - apply() to every column of every dataframe of an ExcelFile, Pandas 如何在没有 for 循环的情况下对每一列应用 function 从现有的创建一个新的 pandas DataFrame? - How can I create a new pandas DataFrame out of an existing one applying a function to every column without a for loop? 我可以在没有 for 循环的情况下将 function 应用于 Pandas dataframe 中的多个列吗? - Can I apply a function to multiple columns in Pandas dataframe without a for loop? 在没有 for 循环的情况下,将包含 if 的函数应用于 pandas 中数据帧的每一行 - Apply a function including if to each row of a dataframe in pandas without for loop 将函数应用于pandas数据框的列 - Apply a function to column of pandas dataframe Pandas - 将逻辑应用于 DataFrame 中的每一列 - Pandas - Apply logic to every column in DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM