pandas 熊猫,将系列连接到 DF 作为行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21004993/
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
Pandas, concat Series to DF as rows
提问by Dick Eshelman
I attempting to add a Series to an empty DataFrame and can not find an answer either in the Doc's or other questions. Since you can append two DataFrames by row or by column it would seem there must be an "axis marker" missing from a Series. Can anyone explain why this does not work?.
我试图将一个系列添加到一个空的 DataFrame 中,但在文档或其他问题中都找不到答案。由于您可以按行或按列附加两个数据帧,因此似乎系列中必须缺少“轴标记”。谁能解释为什么这不起作用?
import Pandas as pd
df1 = pd.DataFrame()
s1 = pd.Series(['a',5,6])
df1 = pd.concat([df1,s1],axis = 1)
#go run some process return s2, s3, sn ...
s2 = pd.Series(['b',8,9])
df1 = pd.concat([df1,s2],axis = 1)
s3 = pd.Series(['c',10,11])
df1 = pd.concat([df1,s3],axis = 1)
If my example above is some how misleading perhaps using the example from the docs will help.
Quoting: Appending rows to a DataFrame.
While not especially efficient (since a new object must be created), you can append a single row to a DataFrame by passing a Series or dict to append, which returns a new DataFrame as above. End Quote.The example from the docs appends "S", which is a row from a DataFrame, "S1" is a Series and attempting to append "S1" produces an error. My question is WHY will appending "S1 not work? The assumption behind the question is that a DataFrame must code or contain axes information for two axes, where a Series must contain only information for one axes.
df = pd.DataFrame(np.random.randn(8, 4), columns=['A','B','C','D']) s = df.xs(3); #third row of DataFrame s1 = pd.Series([np.random.randn(4)]); #new Series of equal len df= df.append(s, ignore_index=True)
如果我上面的例子有点误导,那么使用文档中的例子可能会有所帮助。
引用:将行附加到 DataFrame。
虽然效率不是特别高(因为必须创建一个新对象),但您可以通过将 Series 或 dict 传递给 append 来将单行附加到 DataFrame,这将返回一个新的 DataFrame 如上所述。结束报价。文档中的示例附加了“S”,它是来自 DataFrame 的一行,“S1”是一个系列,尝试附加“S1”会产生错误。我的问题是为什么会附加“S1 不起作用?问题背后的假设是 DataFrame 必须编码或包含两个轴的轴信息,其中 Series 必须只包含一个轴的信息。
df = pd.DataFrame(np.random.randn(8, 4), columns=['A','B','C','D']) s = df.xs(3); #third row of DataFrame s1 = pd.Series([np.random.randn(4)]); #new Series of equal len df= df.append(s, ignore_index=True)
Result
结果
0 1
0 a b
1 5 8
2 6 9
Desired
想要的
0 1 2
0 a 5 6
1 b 8 9
回答by TomAugspurger
You were close, just transposed the result from concat
你很接近,只是将结果从 concat
In [14]: s1
Out[14]:
0 a
1 5
2 6
dtype: object
In [15]: s2
Out[15]:
0 b
1 8
2 9
dtype: object
In [16]: pd.concat([s1, s2], axis=1).T
Out[16]:
0 1 2
0 a 5 6
1 b 8 9
[2 rows x 3 columns]
You also don't need to create the empty DataFrame.
您也不需要创建空的DataFrame.
回答by Dick Eshelman
A method of accomplishing the same objective as appending a Series to a DataFrame is to just convert the data to an array of lists and append the array(s) to the DataFrame.
实现与将 Series 附加到 DataFrame 相同目标的方法是将数据转换为列表数组并将数组附加到 DataFrame。
data as an array of lists
数据作为列表数组
def get_example(idx):
list1 = (idx+1,idx+2 ,chr(idx + 97)) data = [list1] return(data)df1 = pd.DataFrame()
for idx in range(4):
data = get_example(idx) df1= df1.append(data, ignore_index = True)
def get_example(idx):
list1 = (idx+1,idx+2 ,chr(idx + 97)) data = [list1] return(data)df1 = pd.DataFrame()
对于范围内的 idx(4):
data = get_example(idx) df1= df1.append(data, ignore_index = True)

