使用 Pandas 中的系列连接 DataFrame

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

concat a DataFrame with a Series in Pandas

pythonpandas

提问by cacert

Can someone explain what is wrong with this pandas concat code, and why data frame remains empty ?I am using anaconda distibution, and as far as I remember it was working before. enter image description here

有人可以解释一下这个 Pandas concat 代码有什么问题,为什么数据框仍然是空的?我正在使用 anaconda distibution,据我所知,它以前是有效的。 在此处输入图片说明

采纳答案by bakkal

You want to use this form:

你想使用这个表格:

result = pd.concat([dataframe, series], axis=1)

The pd.concat(...)doesn't happen "inplace" into the original dataframebut it would returnthe concatenated result so you'll want to assign the concatenation somewhere, e.g.:

pd.concat(...)不会发生“就地”成原来的dataframe,但它会返回连接的结果,所以你要分配的级联的地方,如:

>>> import pandas as pd
>>> s = pd.Series([1,2,3])
>>> df = pd.DataFrame()
>>> df = pd.concat([df, s], axis=1)  # We assign the result back into df
>>> df
   0
0  1
1  2
2  3