如何从python中的行列表中选择特定的元素?

时间:2022-01-07 21:23:27

I have the following series (df):

我有以下系列(df):

Index    Information
1        [2, A, C]
2        [3, B, C]
3        [4, C, H]
4        [5, D, H]
5        [6, E, H]
6        [7, F, H]

and I want a series that only extracts and stores the third value of each list :

我想要一个只提取和存储每个列表的第三个值的系列:

Index    Information
1        [C]
2        [C]
3        [H]
4        [H]
5        [H]
6        [H]

If I try df[0][2], it correctly gives the required output [C].

如果我尝试df[0][2],它会正确地给出所需的输出[C]。

however, if I try df[:][2], instead of giving

然而,如果我尝试df[:][2],而不是给予。

[C]
[C]
[H]
[H]
[H]
[H]

the output is

输出是

3        [4, C, H]

What should be the correct syntax for this?

正确的语法应该是什么?

2 个解决方案

#1


2  

pandas.Series.str

df.Information.str[2:3]

0    [C]
1    [C]
2    [H]
3    [H]
4    [H]
5    [H]
Name: Information, dtype: object

With assign

df.assign(Information=df.Information.str[2:3])

   Index Information
0      1         [C]
1      2         [C]
2      3         [H]
3      4         [H]
4      5         [H]
5      6         [H]

comprehension per @coldspeed

df.assign(Information=[l[2:3] for l in df.Information.tolist()])

   Index Information
0      1         [C]
1      2         [C]
2      3         [H]
3      4         [H]
4      5         [H]
5      6         [H]

#2


0  

Another alternative:

另一个选择:

df["new_col"] = df["Information"].apply(lambda x: x[2])

#1


2  

pandas.Series.str

df.Information.str[2:3]

0    [C]
1    [C]
2    [H]
3    [H]
4    [H]
5    [H]
Name: Information, dtype: object

With assign

df.assign(Information=df.Information.str[2:3])

   Index Information
0      1         [C]
1      2         [C]
2      3         [H]
3      4         [H]
4      5         [H]
5      6         [H]

comprehension per @coldspeed

df.assign(Information=[l[2:3] for l in df.Information.tolist()])

   Index Information
0      1         [C]
1      2         [C]
2      3         [H]
3      4         [H]
4      5         [H]
5      6         [H]

#2


0  

Another alternative:

另一个选择:

df["new_col"] = df["Information"].apply(lambda x: x[2])