pandas 将数据框转换为列表列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42092585/
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
convert dataframe to list of lists
提问by lolix
I have this dataframe:
我有这个数据框:
value
time
2016-12-01 00:00:00 NaN
2016-12-01 00:01:00 2
2016-12-01 00:02:00 1
I want convert this dataframe to a list of lists, likes this:
我想将此数据框转换为列表列表,如下所示:
[['2016-12-01 00:00:00', NaN],['2016-12-01 00:01:00', 2],['2016-12-01 00:02:00', 1]]
I tried df.values
or df.values.tolist()
and always receive:
我尝试df.values
或df.values.tolist()
总是收到:
[[ nan]
[ 2]
[ 1]]
回答by EdChum
If you call reset_index
to restore the index, you can then call .values.tolist()
to get the desired result:
如果调用reset_index
恢复索引,则可以调用.values.tolist()
得到想要的结果:
In [117]:
df.reset_index().values.tolist()
Out[117]:
[['2016-12-01 00:00:00', nan],
['2016-12-01 00:01:00', 2.0],
['2016-12-01 00:02:00', 1.0]]
values
returns a numpy array, this has a method tolist()
to convert to a list of lists
回答by jezrael
Need DataFrame.reset_index
first, DataFrame.values
return numpy array
and tolist()
convert it to nested list
:
极品DataFrame.reset_index
第一,DataFrame.values
回报numpy array
和tolist()
其转换为nested list
:
print (df.index)
Index(['2016-12-01 00:00:00', '2016-12-01 00:01:00', '2016-12-01 00:02:00'],
dtype='object', name='time')
print (df.reset_index().values.tolist())
[['2016-12-01 00:00:00', nan], ['2016-12-01 00:01:00', 2.0], ['2016-12-01 00:02:00', 1.0]]
But if DatetimeIndex
is necessary convert index
to string
by astype
:
但是,如果DatetimeIndex
有必要转换index
到string
通过astype
:
print (df.index)
DatetimeIndex(['2016-12-01 00:00:00', '2016-12-01 00:01:00',
'2016-12-01 00:02:00'],
dtype='datetime64[ns]', name='time', freq=None)
print (df.reset_index().values.tolist())
[[Timestamp('2016-12-01 00:00:00'), nan],
[Timestamp('2016-12-01 00:01:00'), 2.0],
[Timestamp('2016-12-01 00:02:00'), 1.0]]
df.index = df.index.astype(str)
print (df.reset_index().values.tolist())
[['2016-12-01 00:00:00', nan], ['2016-12-01 00:01:00', 2.0], ['2016-12-01 00:02:00', 1.0]]