[英]How to concatenate multiple Pandas Series into one row?
Goal: I have two Pandas Series
. 目标:我有两个Pandas Series
。 On each I want to apply a function that gives me some summarizing statistic for the column (like sum
, count
and so on). 我想在每个函数上应用一个函数,该函数为列提供一些汇总统计信息(例如sum
, count
等)。 All this is embedded in a for each` loop. 所有这些都嵌入在for``循环中。 Eg: 例如:
DataFrame1
Id V1 V2
0 3 2
1 2 1
DataFrame2
Id T1 T2
0 4 2
1 5 2
The result (on a count task) suppose to be: 结果(在计数任务上)假定为:
DataFrameGoal
Id V1 V2 T1 T2
0 2 2 2 2
My code works fine so for but the solution I get is: 我的代码工作正常,因此,但我得到的解决方案是:
DataFrameGoal
Id V1 V2 T1 T2
0 2 2 NaN NaN
1 NaN NaN 2 2
My code: 我的代码:
import pandas as pd
import numpy as np
df1 = pd.DataFrame({'a' : np.random.randn(6),
'b' : np.random.randn(6),
'c' : np.random.randn(6)})
df2 = pd.DataFrame({'d' : np.random.randn(6),
'e' : np.random.randn(6),
'f' : np.random.randn(6)})
def mysum(col):
return col.count()
lst = []
lst.append(df1)
lst.append(df2)
myDf = pd.DataFrame()
for el in lst:
test = el.apply(lambda cols: mysum(cols))
myDf = myDf.append(test, ignore_index=True)
print(myDf)
Can anyone help me with getting the result I am aiming for? 谁能帮助我获得我想要的结果? I also tried .assign
but this could not solve my problem as well. 我也尝试过.assign
但这也无法解决我的问题。 PS: I know that simple things like count or sum can be accomplished quite easy but I have some complicated task and this is just an easy example. PS:我知道简单的事情(例如计数或总和)可以很容易地完成,但是我有一些复杂的任务,这只是一个简单的例子。
Try this 尝试这个
pd.concat([df1,df2], axis=1)
And then apply whatever function you want to. 然后应用您想要的任何功能。
It's hard to say if the problem is from concatenating dataframes or form mySum()
. 很难说问题出在连接数据帧还是形成mySum()
。 But you can try: 但是您可以尝试:
myDf = (pd.concat(el.apply(lambda cols: mySum(cols))
for el in [df1,df2])
.to_frame().T)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.