gpt4 book ai didi

python - 将列添加到没有标题的 Pandas

转载 作者:太空宇宙 更新时间:2023-11-03 14:56:06 26 4
gpt4 key购买 nike

如何将一列常量值附加到没有标题的 Pandas 数据框?我想在末尾附加该列。

有了标题我可以做到this方式:

df['new'] = pd.Series([0 for x in range(len(df.index))], index=df.index)

最佳答案

每个都不为空DataFrame索引和一些值。

您可以添加默认列值并创建由标量填充的新列:

df[len(df.columns)] = 0

示例:

df = pd.DataFrame({0:[1,2,3],
1:[4,5,6]})

print (df)
0 1
0 1 4
1 2 5
2 3 6

df[len(df.columns)] = 0
print (df)
0 1 2
0 1 4 0
1 2 5 0
2 3 6 0

另外,对于创建具有名称的新列,最简单的是:

df['new'] = 1

关于python - 将列添加到没有标题的 Pandas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42473098/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com