Python 将数组列表作为列附加到具有相同列索引的 Pandas Data Frame
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29088646/
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
Append a list of arrays as column to pandas Data Frame with same column indices
提问by PyLearner
I have a list of arrays (one-dimensional numpy array) (a_) and a list (l_) and want to have a DataFrame with them as its columns. They look like this:
我有一个数组列表(一维 numpy 数组)(a_)和一个列表(l_),并希望将它们作为列的 DataFrame。它们看起来像这样:
a_: [array([381]), array([376]), array([402]), array([400])...]
l_: [1.5,2.34,4.22,...]
I can do it by:
我可以通过以下方式做到:
df_l = pd.DataFrame(l_) df_a = pd.DataFrame(a_) df = pd.concat([df_l, df_a], axis=1)
df_l = pd.DataFrame(l_) df_a = pd.DataFrame(a_) df = pd.concat([df_l, df_a], axis=1)
Is there a shorterway of doing it? I tried to use pd.append
:
有没有更短的方法?我尝试使用pd.append
:
df_l = pd.DataFrame(l_) df_l = df_l.append(a_)
df_l = pd.DataFrame(l_) df_l = df_l.append(a_)
However, because columns indices are both 0, it adds a_ to the end of the dataframe column, resulting in a single column. Is there something like this:
但是,由于列索引都是 0,因此它将 a_ 添加到数据框列的末尾,从而生成单个列。有没有这样的:
l_ = l_.append(a_).reset(columns)
l_ = l_.append(a_).reset(columns)
that set a new column index for the appended array? well, obviously this does not work!
为附加数组设置新的列索引?好吧,显然这行不通!
the desired output is like:
所需的输出是这样的:
0 0 0 1.50 381 1 2.34 376 2 4.22 402
...
0 0 0 1.50 381 1 2.34 376 2 4.22 402
...
Thanks.
谢谢。
采纳答案by kennes
Suggestion:
建议:
df_l = pd.DataFrame(l_)
df_1['a_'] = pd.Series(a_list, index=df_1.index)
Example #1:
示例#1:
L = list(data)
A = list(data)
data_frame = pd.DataFrame(L)
data_frame['A'] = pd.Series(A, index=data_frame.index)
Example #2 - Same Series length (create series and set index to the same as existing data frame):
示例 #2 - 相同的系列长度(创建系列并将索引设置为与现有数据框相同):
In [33]: L = list(item for item in range(10))
In [34]: A = list(item for item in range(10,20))
In [35]: data_frame = pd.DataFrame(L,columns=['L'])
In [36]: data_frame['A'] = pd.Series(A, index=data_frame.index)
In [37]: print data_frame
L A
0 0 10
1 1 11
2 2 12
3 3 13
4 4 14
5 5 15
6 6 16
7 7 17
8 8 18
9 9 19
Example #3 - Different Series lengths (create series and let pandas handle index matching):
示例 #3 - 不同的系列长度(创建系列并让熊猫处理索引匹配):
In [45]: not_same_length = list(item for item in range(50,55))
In [46]: data_frame['nsl'] = pd.Series(not_same_length)
In [47]: print data_frame
L A nsl
0 0 10 50
1 1 11 51
2 2 12 52
3 3 13 53
4 4 14 54
5 5 15 NaN
6 6 16 NaN
7 7 17 NaN
8 8 18 NaN
9 9 19 NaN
Based on your comments, it looks like you want to join your list of lists.I'm assuming they are in list structure because array()
is not a method in python. To do that you would do the following:
根据您的评论,您似乎想加入列表列表。我假设它们在列表结构中,因为array()
它不是 Python 中的方法。为此,您将执行以下操作:
In [63]: A = [[381],[376], [402], [400]]
In [64]: A = [inner_item for item in A for inner_item in item]
In [65]: print A
[381, 376, 402, 400]
Then create the Series using the new array and follow the steps above to add to your data frame.
然后使用新数组创建系列并按照上述步骤添加到您的数据框中。