根据条件将数据帧转换为元组列表

时间:2016-09-21 11:35:09

标签: python list pandas dataframe tuples

我有以下df:

'in randul 1
Cells(ActiveCell.Row, 2).Value = Pclient.Value
Cells(ActiveCell.Row, 3).Value = Pcodclient.Value
Cells(ActiveCell.Row, 4).Value = Pptype.Value

如何将其转换为元组列表,并且仅采用大于constructor(db : Database, handler: ( transaction: Transaction) -> Unit) : this(db, Handler<Transaction>( {handler.invoke(it)})) @JvmOverloads constructor(db : Database, handler: ( transaction: Transaction) -> Any) : this(db, Handler<Transaction>( {handler.invoke(it)})) 的值。

例如,我想拥有以下值:

    1         2         3         4 
1  NaN  0.000000  0.000000  0.000000
2  NaN  0.027273  0.000000  0.000000
3  NaN  0.000000  0.101449  0.000000
4  NaN  0.000000  0.000000  0.194245
5  NaN  0.000000  0.000000  0.000000
6  NaN  0.000000  0.000000  0.000000
7  NaN  0.000000  0.000000  0.000000
8  NaN  0.000000  0.000000  0.000000
13 NaN  0.000000  0.000000  0.000000
14 NaN  0.000000  5         0.000000

1 个答案:

答案 0 :(得分:1)

您可以先将列投放到int(如有必要),unstack并使用列表理解,其中必须将tuples中的第一个和第二个值转换为int(默认值为float):

df.columns = df.columns.astype(int)

s = df.unstack()
tuples = [tuple((int(x[0]),int(x[1]),x[2])) for x in s[s>0].reset_index().values]
print (tuples)
[(2, 2, 0.027273000000000002), (3, 3, 0.101449), (3, 14, 5.0), (4, 4, 0.194245)]