Pandas: 将结果作为新 DataFrame 列出现
要将结果作为新的 dataframe 的列出现,可以使用 pandas 库中的 concat() 函数将原始 dataframe 与结果 dataframe 按列连接起来。
以下是一个示例代码:
import pandas as pd
# 原始 dataframe
df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# 结果 dataframe
df2 = pd.DataFrame({'C': [7, 8, 9], 'D': [10, 11, 12]})
# 将结果作为新的 dataframe 的列连接
new_df = pd.concat([df1, df2], axis=1)
# 打印新的 dataframe
print(new_df)
输出结果为:
A B C D
0 1 4 7 10
1 2 5 8 11
2 3 6 9 12
在上述代码中,使用 concat() 函数将 df1 和 df2 按列连接起来,并将结果赋值给 new_df,然后通过打印输出 new_df。
原文地址: https://www.cveoy.top/t/topic/igLg 著作权归作者所有。请勿转载和采集!