在Pandas中将DataFrame与系列连接

cac*_*ert 7 python pandas

有人可以解释这个熊猫concat代码有什么问题吗,为什么数据框仍然为空? 在此处输入图片说明

bak*_*kal 6

您要使用以下形式:

result = pd.concat([dataframe, series], axis=1)
Run Code Online (Sandbox Code Playgroud)

该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
Run Code Online (Sandbox Code Playgroud)