如何用数组既是数组又是普通int的列拆栈熊猫?

时间:2018-09-26 13:25:44

标签: python pandas matrix

给出一个看起来像这样的func TheFunc(c *gin.Context) { GetErrResponseList(c, config.FailureMsg, nil, nil) }

func GetErrResponseList(c *gin.Context, msg string, data, count interface{}) { 
defer c.Request.Body.Close() 
response := ResponseControllerList{400, 0, msg, data, count} 
c.JSON(200, gin.H{ 
config.Response: response, 
}) 
}

我怎样才能拆开它,使它看起来像这样?

pandas dataframe

2 个答案:

答案 0 :(得分:1)

一种简单的方法是求和(假设col1col2个对象是list个)

df.col1 + df.col2 + df.col3.transform(lambda item: [item])

0    [0, 0, 0, 0, 0, 0, 0, 0, 0.265]
1    [0, 0, 0, 0, 0, 0, 0, 0, 0.243]
2    [0, 0, 0, 0, 0, 0, 0, 0, 0.289]
3    [0, 0, 0, 0, 0, 0, 0, 0, 0.213]

对于数组

df.agg(lambda s: np.concatenate(s, axis=None), 1)

请注意,这些操作不会很快。如果打算对具有pandas对象的列执行操作,则也许应该考虑不使用dtype。考虑使用列表列表

答案 1 :(得分:1)

Well there are actually multiple ways to do it. Unfortunately pandas does not have any function like this but you can use other
functions.In case of Python,
I'd prefer to use built-in functions in Reduce, as they would be faster as compared to using a lambda.'
using Reduce function.
import operator
import functools

list = [[1, 2], [2], [2, 3]]
ab=functools.reduce(operator.add, list)
print(ab);

**Output is** 
[1, 2, 2, 2, 3]