熊猫将特定列转换为列表

时间:2018-08-22 13:39:26

标签: python python-3.x pandas

比方说,我有一个看起来像这样的数据框:

df2 = pd.DataFrame(['Apple', 'orange', 'pear', 'Apple'], columns=['A'])

df4 = pd.DataFrame({'Q':['chair', 'desk', 'monitor', 'chair'], 'R':['red', 'blue', 'yellow', 'purple'], 'S': ['english', 'german', 'spanish', 'english']})

df3 = pd.merge(df2, df4, left_index=True, right_index=True)



 df3

        A        Q       R        S
0   Apple    chair     red  english
1  orange     desk    blue   german
2    pear  monitor  yellow  spanish
3   Apple    chair  purple  english

我想做的是创建一个看起来像这样的数组:

df3_array_col_a = ['Apple', 'orange', 'pear', 'Apple']
df3_array_col_b = ['chair', 'desk', 'monitor', 'chair']

有没有办法做到这一点?

我试图做的是这样:

df6 = df3
df6 = df6.columns.get_values()
df6.tolist()

但这给了我这个

['A', 'Q', 'R', 'S']

然后我尝试这样做:

df6 = df6.A.get_values()

但这给了我这个

Traceback (most recent call last):
  File "<pyshell#54>", line 1, in <module>
    df6 = df6.A.get_values()
AttributeError: 'numpy.ndarray' object has no attribute 'A'

0 个答案:

没有答案