Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 熊猫-如何仅将列表的最后一个元素添加到另一列?_Python_Pandas_List_Dataframe - Fatal编程技术网

Python 熊猫-如何仅将列表的最后一个元素添加到另一列?

Python 熊猫-如何仅将列表的最后一个元素添加到另一列?,python,pandas,list,dataframe,Python,Pandas,List,Dataframe,我有一个Pandas数据框,其中一些列中有列表: email A B something@gmail.com [name1, name2] [thing1, thing2, thing3] another@gmail.com [name] [thing1, thing2] 我只希望在每行中有每个列表的最后一个元素,如下所示: email

我有一个Pandas数据框,其中一些列中有列表:

email                         A                      B
something@gmail.com    [name1, name2]    [thing1, thing2, thing3]
another@gmail.com          [name]           [thing1, thing2]
我只希望在每行中有每个列表的最后一个元素,如下所示:

email                    A         B
something@gmail.com    name2    thing3
another@gmail.com      name     thing2
有没有简单的方法?我最初想到的是
data['newcolumn']=data['A'][Number of row][1]
,但我在做“Number of row”部分时有点不知所措。谢谢

假设您的数据帧被称为
df
,您可以执行以下操作

def return\u last\u元素(行):
#如果给定列的行是list或tuple,则获取最后一个元素
如果isinstance(行,(列表,元组)):
返回行[-1]
#否则只返回值
其他:
返回行
#循环所有列,并将函数应用于每列的每一行
对于df.列中的列:
df[col]=df[col].apply(返回最后一个元素)

假设您的数据帧被称为
df
,您可以执行以下操作

def return\u last\u元素(行):
#如果给定列的行是list或tuple,则获取最后一个元素
如果isinstance(行,(列表,元组)):
返回行[-1]
#否则只返回值
其他:
返回行
#循环所有列,并将函数应用于每列的每一行
对于df.列中的列:
df[col]=df[col].apply(返回最后一个元素)

这是我的答案,创建一个名为getLastValue()的简单函数,它获取每行中的列表并返回该列表的最后一个值。见下文

import pandas as pd

data = {
    'email' : ['something@gmail.com ', 'another@gmail.com'],
    'A': [['name1','name2'], ['name']],
    'B': [['thing1', 'thing2', 'thing3'], ['thing1', 'thing2']]
}

def getLastValue(aList):
    return aList[-1]

df = pd.DataFrame(data)

df['A'] = df['A'].apply(getLastValue)
df['B'] = df['B'].apply(getLastValue)

print(df)

  

下面是我的答案,创建一个名为getLastValue()的简单函数,它获取每行中的列表并返回该列表的最后一个值。见下文

import pandas as pd

data = {
    'email' : ['something@gmail.com ', 'another@gmail.com'],
    'A': [['name1','name2'], ['name']],
    'B': [['thing1', 'thing2', 'thing3'], ['thing1', 'thing2']]
}

def getLastValue(aList):
    return aList[-1]

df = pd.DataFrame(data)

df['A'] = df['A'].apply(getLastValue)
df['B'] = df['B'].apply(getLastValue)

print(df)

  

您只需使用
Series.str[-1]

In [145]: df = pd.DataFrame({'email':['something@gmail.com', 'another@gmail.com'], 'A':[['name1', 'name2'], ['name']], 'B':[['thing1', 'thing2', 'thing3'], ['thing1', 'thing2']]})

In [146]: df
Out[146]: 
                 email               A                         B
0  something@gmail.com  [name1, name2]  [thing1, thing2, thing3]
1    another@gmail.com          [name]          [thing1, thing2]

In [148]: df['A'] = df['A'].str[-1]

In [149]: df['B'] = df['B'].str[-1]

In [150]: df
Out[150]: 
                 email      A       B
0  something@gmail.com  name2  thing3
1    another@gmail.com   name  thing2

您只需使用
Series.str[-1]

In [145]: df = pd.DataFrame({'email':['something@gmail.com', 'another@gmail.com'], 'A':[['name1', 'name2'], ['name']], 'B':[['thing1', 'thing2', 'thing3'], ['thing1', 'thing2']]})

In [146]: df
Out[146]: 
                 email               A                         B
0  something@gmail.com  [name1, name2]  [thing1, thing2, thing3]
1    another@gmail.com          [name]          [thing1, thing2]

In [148]: df['A'] = df['A'].str[-1]

In [149]: df['B'] = df['B'].str[-1]

In [150]: df
Out[150]: 
                 email      A       B
0  something@gmail.com  name2  thing3
1    another@gmail.com   name  thing2

此代码可能会帮助您:

data['newA']=[data['A'][i][-1] for i in range(len(data))]
data['newB']=[data['B'][i][-1] for i in range(len(data))]

此代码可能会帮助您:

data['newA']=[data['A'][i][-1] for i in range(len(data))]
data['newB']=[data['B'][i][-1] for i in range(len(data))]