Pandas DataFrame 到列表列表 [英] Pandas DataFrame to List of Lists

查看:55
本文介绍了Pandas DataFrame 到列表列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将列表列表转换为 Pandas 数据框很容易:

将pandas导入为pddf = pd.DataFrame([[1,2,3],[3,4,5]])

但是如何将 df 转回列表列表?

lol = df.what_to_do_now?打印哈哈# [[1,2,3],[3,4,5]]

解决方案

你可以访问底层数组并调用它的 tolist 方法:

<预><代码>>>>df = pd.DataFrame([[1,2,3],[3,4,5]])>>>大声笑 = df.values.tolist()>>>哈哈[[1L、2L、3L]、[3L、4L、5L]]

It's easy to turn a list of lists into a pandas dataframe:

import pandas as pd
df = pd.DataFrame([[1,2,3],[3,4,5]])

But how do I turn df back into a list of lists?

lol = df.what_to_do_now?
print lol
# [[1,2,3],[3,4,5]]

解决方案

You could access the underlying array and call its tolist method:

>>> df = pd.DataFrame([[1,2,3],[3,4,5]])
>>> lol = df.values.tolist()
>>> lol
[[1L, 2L, 3L], [3L, 4L, 5L]]

这篇关于Pandas DataFrame 到列表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆