Python 从函数向 Pandas 数据框添加多列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30026815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add Multiple Columns to Pandas Dataframe from Function
提问by EFL
I have a pandas data frame mydf
that has two columns,and both columns are datetime datatypes: mydate
and mytime
. I want to add three more columns: hour
, weekday
, and weeknum
.
我有一个mydf
有两列的熊猫数据框,两列都是日期时间数据类型:mydate
和mytime
。我要添加三个多列:hour
,weekday
,和weeknum
。
def getH(t): #gives the hour
return t.hour
def getW(d): #gives the week number
return d.isocalendar()[1]
def getD(d): #gives the weekday
return d.weekday() # 0 for Monday, 6 for Sunday
mydf["hour"] = mydf.apply(lambda row:getH(row["mytime"]), axis=1)
mydf["weekday"] = mydf.apply(lambda row:getD(row["mydate"]), axis=1)
mydf["weeknum"] = mydf.apply(lambda row:getW(row["mydate"]), axis=1)
The snippet works, but it's not computationally efficient as it loops through the data frame at least three times. I would just like to know if there's a faster and/or more optimal way to do this. For example, using zip
or merge
? If, for example, I just create one function that returns three elements, how should I implement this? To illustrate, the function would be:
该代码段有效,但计算效率不高,因为它至少循环了数据帧 3 次。我只想知道是否有更快和/或更优的方法来做到这一点。例如,使用zip
或merge
? 例如,如果我只创建一个返回三个元素的函数,我应该如何实现它?为了说明,该功能将是:
def getHWd(d,t):
return t.hour, d.isocalendar()[1], d.weekday()
采纳答案by Zero
Here's on approach to do it using one apply
这是使用一个方法来做到这一点 apply
Say, df
is like
说,df
就像
In [64]: df
Out[64]:
mydate mytime
0 2011-01-01 2011-11-14
1 2011-01-02 2011-11-15
2 2011-01-03 2011-11-16
3 2011-01-04 2011-11-17
4 2011-01-05 2011-11-18
5 2011-01-06 2011-11-19
6 2011-01-07 2011-11-20
7 2011-01-08 2011-11-21
8 2011-01-09 2011-11-22
9 2011-01-10 2011-11-23
10 2011-01-11 2011-11-24
11 2011-01-12 2011-11-25
We'll take the lambda function out to separate line for readability and define it like
我们将把 lambda 函数取出来分隔行以提高可读性并将其定义为
In [65]: lambdafunc = lambda x: pd.Series([x['mytime'].hour,
x['mydate'].isocalendar()[1],
x['mydate'].weekday()])
And, apply
and store the result to df[['hour', 'weekday', 'weeknum']]
并且,apply
并将结果存储到df[['hour', 'weekday', 'weeknum']]
In [66]: df[['hour', 'weekday', 'weeknum']] = df.apply(lambdafunc, axis=1)
And, the output is like
而且,输出就像
In [67]: df
Out[67]:
mydate mytime hour weekday weeknum
0 2011-01-01 2011-11-14 0 52 5
1 2011-01-02 2011-11-15 0 52 6
2 2011-01-03 2011-11-16 0 1 0
3 2011-01-04 2011-11-17 0 1 1
4 2011-01-05 2011-11-18 0 1 2
5 2011-01-06 2011-11-19 0 1 3
6 2011-01-07 2011-11-20 0 1 4
7 2011-01-08 2011-11-21 0 1 5
8 2011-01-09 2011-11-22 0 1 6
9 2011-01-10 2011-11-23 0 2 0
10 2011-01-11 2011-11-24 0 2 1
11 2011-01-12 2011-11-25 0 2 2
回答by Venkat R
def getWd(d):
d.isocalendar()[1], d.weekday()
def getH(t):
return t.hour
mydf["hour"] = zip(*df["mytime"].map(getH))
mydf["weekday"], mydf["weeknum"] = zip(*df["mydate"].map(getWd))
回答by Pedro M Duarte
To complement John Galt's answer:
补充约翰高尔特的回答:
Depending on the task that is performed by lambdafunc
, you may experience some speedup by storing the result of apply
in a new DataFrame
and then joining with the original:
根据 执行的任务lambdafunc
,您可能会通过将 的结果存储apply
在新的中DataFrame
然后与原始连接来体验一些加速:
lambdafunc = lambda x: pd.Series([x['mytime'].hour,
x['mydate'].isocalendar()[1],
x['mydate'].weekday()])
newcols = df.apply(lambdafunc, axis=1)
newcols.columns = ['hour', 'weekday', 'weeknum']
newdf = df.join(newcols)
Even if you do not see a speed improvement, I would recommend using the join
. You will be able to avoid the (always annoying) SettingWithCopyWarning
that may pop up when assigning directly on the columns:
即使您没有看到速度提升,我也建议您使用join
. 您将能够避免SettingWithCopyWarning
直接在列上分配时可能弹出的(总是令人讨厌的):
SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
回答by samsamoa
You can do this in a somewhat cleaner method by having the function you apply return a pd.Series
with named elements:
您可以通过让您应用的函数返回pd.Series
带有命名元素的a以更简洁的方法执行此操作:
def process(row):
return pd.Series(dict(b=row["a"] * 2, c=row["a"] + 2))
my_df = pd.DataFrame(dict(a=range(10)))
new_df = my_df.join(my_df.apply(process, axis="columns"))
The result is:
结果是:
a b c
0 0 0 2
1 1 2 3
2 2 4 4
3 3 6 5
4 4 8 6
5 5 10 7
6 6 12 8
7 7 14 9
8 8 16 10
9 9 18 11