Python 将二维数组转换为两列数据框熊猫

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

turning a two dimensional array into a two column dataframe pandas

pythonpandas

提问by user3314418

if I have the following, how do I make pd.DataFrame() turn this array into a dataframe with two columns. What's the most efficient way? My current approach involves creating copies out of each into a series and making dataframes out of them.

如果我有以下内容,我如何使 pd.DataFrame() 将此数组转换为具有两列的数据框。最有效的方法是什么?我目前的方法涉及将每个副本创建为一个系列并从中制作数据帧。

From this:

由此:

([[u'294 (24%) L', u'294 (26%) R'],
  [u'981 (71%) L', u'981 (82%) R'],])

to

x    y
294  294
981  981

rather than

而不是

x
[u'294 (24%) L', u'294 (26%) R']

my current approach. Looking for something more efficient

我目前的做法。寻找更有效的东西

numL = pd.Series(numlist).map(lambda x: x[0])
    numR = pd.Series(numlist).map(lambda x: x[1])

    nL = pd.DataFrame(numL, columns=['left_num'])
    nR = pd.DataFrame(numR, columns=['right_num'])

    nLR = nL.join(nR)

    nLR

UPDATE**

更新**

I noticed that my error simply comes down to when you pd.DataFrame() a list versus a series. WHen you create a dataframe out of a list, it merges the items into the same column. Not so with a list. That solved my problem in the most efficient way.

我注意到我的错误只是归结为 pd.DataFrame() 是列表还是系列。当您从列表中创建数据框时,它会将项目合并到同一列中。列表不是这样。这以最有效的方式解决了我的问题。

采纳答案by unutbu

In [172]: data = [[u'294 (24%) L', u'294 (26%) R'],  [u'981 (71%) L', u'981 (82%) R'],]

In [173]: clean_data = [[int(item.split()[0]) for item in row] for row in data]

In [174]: clean_data
Out[174]: [[294, 294], [981, 981]]

In [175]: pd.DataFrame(clean_data, columns=list('xy'))
Out[175]: 
     x    y
0  294  294
1  981  981

[2 rows x 2 columns]

回答by Harvey

To upgrade on @unutbu answer because I don't think that code will work because of this argument:

要升级@unutbu 答案,因为我认为代码不会因为这个论点而工作:

columns=list('xy')

So I think it should be like this incorrect argument:

所以我认为应该是这个不正确的论点:

pd.DataFrame(clean_data, columns=['x', 'y'])

Taken from official docs.

取自官方文档