pandas 将一个 DataFrame 行转换为平面列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34238391/
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 00:22:34  来源:igfitidea点击:

Convert one DataFrame row to flat list

pythonpandas

提问by Jacob H

I new to Python and I'm therefore having trouble converting a row in a DataFrameinto a flat list. To do this I use the following code:

我是 Python 新手,因此无法将 a 中的一行DataFrame转换为 flat list。为此,我使用以下代码:

Toy DataFrame:

玩具DataFrame

import pandas as pd
d = {
     "a": [1, 2, 3, 4, 5],
     "b": [9, 8, 7, 6, 5],
     "n": ["a", "b", "c", "d", "e"]
}

df = pd.DataFrame(d)

My code:

我的代码:

df_note = df.loc[df.n == "d", ["a", "b"]].values #convert to array
df_note = df_note.tolist() #convert to nested list
df_note = reduce(lambda x, y: x + y, df_note) #convert to flat list

To me this code appears to be both gross and inefficient. The fact that I convert to an arraybefore a listis what is causing the problem, i.e. the listto be nested. That withstanding, I can not find a means of converting the row directly to a list. Any advice?

对我来说,这段代码既粗俗又低效。我array在 a 之前转换为a的事实list是导致问题的原因,即list要嵌套的。尽管如此,我找不到将行直接转换为列表的方法。有什么建议吗?

This question is not a dupe of this. In my case, I want the list to be flat.

这个问题不是这个。就我而言,我希望列表是扁平的。

回答by Colonel Beauvel

You are almost there, actually just use flatteninstead of reduceto unnest the array (instead of unnesting the list), and chain operations to have a one liner:

你快到了,实际上只是使用flatten而不是reduce取消嵌套数组(而不是取消嵌套列表)和链操作以获得一个单行:

df.loc[df.n == "d", ['a','b']].values.flatten().tolist()
#[4, 6]

回答by Mike Müller

You get a nested list because you select a sub data frame.

因为您选择了一个子数据框,所以您会得到一个嵌套列表。

This takes a row, which can be converted to a list without flattening:

这需要一行,可以将其转换为列表而无需展平:

df.loc[0, :].values.tolist()
[1, 9, 'a']

How about slicing the list:

如何切片列表:

df_note.values.tolist()[0]
[4, 6]

The values are stored in an NumPy array. So you do not convert them. Pandas uses a lot of NumPy under the hood. The attribute access df_note.valuesis just a different name for part of the data frame.

这些值存储在 NumPy 数组中。所以你不转换它们。Pandas 在底层使用了大量 NumPy。属性访问df_note.values只是部分数据框的不同名称。

回答by Takis

I am assuming you're explicitly selecting columns aand bonly to get rid of column n, which you are solely using to select the wanted row.

我假设您明确选择了列a并且b只是为了摆脱 column n,而您仅使用它来选择所需的行。

In that case, you could also use the ncolumn as the index first, using set_index:

在这种情况下,您还可以首先使用该n列作为索引,使用set_index

>>> dfi = df.set_index('n')
>>> dfi.ix['d'].tolist()
[4, 6]