从pandas dataframe列中提取特定值

时间:2018-04-29 20:38:19

标签: python pandas dataframe jupyter-notebook

我在pandas数据框列中有一个数据如下:

[2, 4]
[3, 4]
[1, 4]
[0, 0] 

我希望数据采用

的形式
col_1  col_2
2      4
3      4
1      4
0      0

任何人都可以帮助我如何以上述形式获取数据。

3 个答案:

答案 0 :(得分:2)

如果列表都具有相同数量的元素,您可以使用.tolist()来轻松完成此操作

import pandas as pd   
df = pd.DataFrame({'val1': [[2, 4], [3, 4], [1, 4], [0, 0]]})   

df[['col_1', 'col_2']] = pd.DataFrame(df.val1.tolist())
     val1  col_1  col_2
0  [2, 4]      2      4
1  [3, 4]      3      4
2  [1, 4]      1      4
3  [0, 0]      0      0

答案 1 :(得分:1)

ALollz提供了更好的答案,但鉴于提取特定值的标题,这里有一个更直接,更具说明性(但效率更低)的方法:

import pandas as pd

df = pd.DataFrame()
df["cur"] = [[2,4],[3,4],[1,4],[0,0]]
print(df) # This is what you have

# You can access elements by df[<column>][<row>][<list index>]
# This is looping across all rows of the "cur" column, and pulling out
#    the values at the 0th and 1st index.
df["col_1"] = [pair[0] for pair in df["cur"]]
df["col_2"] = [pair[1] for pair in df["cur"]]
print(df)

<强>输出

      cur
0  [2, 4]
1  [3, 4]
2  [1, 4]
3  [0, 0]
      cur  col1  col2
0  [2, 4]     2     4
1  [3, 4]     3     4
2  [1, 4]     1     4
3  [0, 0]     0     0

答案 2 :(得分:0)

另一种方法是使用applypd.Series

df = pd.DataFrame({'val1': [[2, 4], [3, 4], [1, 4], [0, 0]]})   

df['val1'].apply(pd.Series).rename(columns=lambda x: x + 1).add_prefix('col_')

输出:

   col_1  col_2
0      2      4
1      3      4
2      1      4
3      0      0

或类似于@ALollz,但更强大,可以捕获任意数量的列。

pd.DataFrame(df['val1'].tolist())\
  .rename(columns=lambda x: x + 1)\
  .add_prefix('col_')
相关问题