pandas 将数据框列中的值附加到列表

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

Append values from dataframe column to list

pythonpandaslistappend

提问by Javier López Tomás

I have a dataframe with several columns, and I want to append to an empty list the values of one column, so that the desired output would be the following:

我有一个包含多列的数据框,我想将一列的值附加到一个空列表中,以便所需的输出如下:

empty_list = [value_1,value_2,value_3...]

I have tried the following:

我尝试了以下方法:

df = pd.DataFrame({'country':['a','b','c','d'],
      'gdp':[1,2,3,4],
      'iso':['x','y','z','w']})
a_list = []

a_list.append(df['iso'])
a_list.append(df['iso'].values)
a_list.append(df['iso'].tolist())

Either way, I get a list with lists, numpy arrays or series inside it, and I would like to have directly the records.

无论哪种方式,我都会得到一个包含列表、numpy 数组或系列的列表,我想直接获取记录。

回答by jezrael

You could try this script if you need to append one column only:

如果您只需要附加一列,您可以试试这个脚本:

a_list = df['iso'].tolist()

For extending a list by appending elements from the iterable, use extend:

要通过添加可迭代元素来扩展列表,请使用extend

a_list = []
a_list.extend(df['iso'].tolist())
a_list.extend(df['country'].tolist())
print (a_list)
['x', 'y', 'z', 'w', 'a', 'b', 'c', 'd']

Another solution is to use numpy.ravelwith transpose:

另一种解决方案是numpy.ravel与转置一起使用:

a_list = df[['iso','country']].values.T.ravel().tolist()
print (a_list)
['x', 'y', 'z', 'w', 'a', 'b', 'c', 'd']

回答by balkon16

Your problem arises from the fact that df['iso'].tolist()creates a list. The list is appended (given a place in the list at the single index), so you get a list of list. You can try:

您的问题源于df['iso'].tolist()创建列表的事实。该列表已附加(在列表中的单个索引处给出一个位置),因此您将获得一个列表列表。你可以试试:

a_list.extend(df['iso'].tolist())

回答by anky

extenddoes what you ask for . If you try do this with append, you can do something like:

extend做你所要求的。如果您尝试使用 执行此操作append,您可以执行以下操作:

import itertools
a_list = []
a_list.append(df.iso.tolist())
a_list.append(df.country.tolist())
a_list=list(itertools.chain.from_iterable(a_list))
print(a_list)


Output

输出

['x', 'y', 'z', 'w', 'a', 'b', 'c', 'd']

回答by Santosh

To access the data of each row of the Pandas dataframe we can use DataFrame.iat attribute and then we can append the data of each row to the end of the list. In first for loop iterate over each row and create a list to store the data of the current row In second for loop iterate over all the columns and append the data of each column to the list after that append the current row to the list

要访问 Pandas 数据框每一行的数据,我们可以使用 DataFrame.iat 属性,然后我们可以将每一行的数据附加到列表的末尾。在第一个 for 循环迭代每一行并创建一个列表来存储当前行的数据第二个 for 循环迭代所有列并将每列的数据附加到列表之后将当前行附加到列表

df = pd.DataFrame({'country':['a','b','c','d'],'gdp':[1,2,3,4],'iso':['x','y','z','w']})
a_list = []
for i in range((df.shape[0])):
cur_row =[]
for j in range(df.shape[1]):
    cur_row.append(df.iat[i, j])            
a_list.append(cur_row) 

回答by SimbaPK

This example should be enough:

这个例子应该足够了:

myList = df['iso'].tolist() 
print(myList)

Output:

输出:

['x', 'y', 'z', 'w']

['x', 'y', 'z', 'w']