访问 Pandas DataFrame 元素内的列表

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

Access a list within an element of a Pandas DataFrame

pythonlistpandasindexingdataframe

提问by Michael

I have a Pandas DataFramewhich has a list of integers inside one of the columns. I'd like to access the individual elements within this list. I've found a way to do it by using tolist()and turning it back into a DataFrame, but I am wondering if there is a simpler/better way. In this example, I add Column Ato the middle element of the list in Column B.

我有一个 Pandas DataFrame,其中一列中有一个整数列表。我想访问此列表中的各个元素。我找到了一种方法,通过使用tolist()并将其转换回DataFrame,但我想知道是否有更简单/更好的方法。在此示例中,我将 Column 添加A到Column中列表的中间元素B

import pandas as pd
df = pd.DataFrame({'A' : (1,2,3), 'B': ([0,1,2],[3,4,5,],[6,7,8])})
df['C'] = df['A'] + pd.DataFrame(df['B'].tolist())[1]
df

Is there a better way to do this?

有一个更好的方法吗?

回答by breucopter

A bit more straightforward is:

更直接一点的是:

df['C'] = df['A'] + df['B'].apply(lambda x:x[1])

回答by Meilin He

You can also simply try the following:

您也可以简单地尝试以下操作:

df['C'] = df['A'] + df['B'].str[1]

Performance of this method:

该方法的性能:

%timeit df['C'] = df['A'] + df['B'].str[1]
#1000 loops, best of 3: 445 μs per loop

回答by Psidom

One option is to use the apply, which should be faster than creating a data frame out of it:

一种选择是使用apply,它应该比从中创建数据框更快:

df['C'] = df['A'] + df.apply(lambda row: row['B'][1], axis = 1) 

Some speed test:

一些速度测试:

%timeit df['C'] = df['A'] + pd.DataFrame(df['B'].tolist())[1]
# 1000 loops, best of 3: 567 μs per loop
%timeit df['C'] = df['A'] + df.apply(lambda row: row['B'][1], axis = 1) 
# 1000 loops, best of 3: 406 μs per loop
%timeit df['C'] = df['A'] + df['B'].apply(lambda x:x[1])
# 1000 loops, best of 3: 250 μs per loop

OK. Slightly better. @breucopter's answer is the fastest.

好的。稍微好一些。@breucopter 的答案是最快的。