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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:54:49  来源:igfitidea点击:

convert dataframe to list of lists

pythonpandas

提问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.valuesor df.values.tolist()and always receive:

我尝试df.valuesdf.values.tolist()总是收到:

[[ nan]
 [   2]
 [   1]]

回答by EdChum

If you call reset_indexto 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]]

valuesreturns a numpy array, this has a method tolist()to convert to a list of lists

values返回一个 numpy 数组,它有一个方法tolist()可以转换为列表列表

回答by jezrael

Need DataFrame.reset_indexfirst, DataFrame.valuesreturn numpy arrayand tolist()convert it to nested list:

极品DataFrame.reset_index第一,DataFrame.values回报numpy arraytolist()其转换为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 DatetimeIndexis necessary convert indexto stringby astype:

但是,如果DatetimeIndex有必要转换indexstring通过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]]