gpt4 book ai didi

python - 将 header 添加到 DataFrame Pandas

转载 作者:太空狗 更新时间:2023-10-30 02:07:18 24 4
gpt4 key购买 nike

我有一个数据框:

Index   1   Dr. Santosh Kumar
0 NaN BHR/ 6681/148/1/134094/2007-08/L
1 NaN B/301, Laxmi Apartment
2 NaN New Chitragupta Nagar, Kankerbagh
3 NaN Patna – 800 020
4 NaN NaN
5 2 Dr. Deepak Kumar
6 NaN BHR/ 6682/148/2/134095/2007-08/L
7 NaN At & P.o- Bairia
8 NaN P.s- Gourichak
9 NaN Patna – 800 007

我想为这个数据框添加一个标题,

df = pd.DataFrame([df],columns = ["id","information"])

但是我得到这个错误:

ValueError: Shape of passed values is (1, 1), indices imply (2, 1)

所以最终输出应该是:

Index   id  information
0 1 Dr. Santosh Kumar
1 NaN BHR/ 6681/148/1/134094/2007-08/L
2 NaN B/301, Laxmi Apartment
3 NaN New Chitragupta Nagar, Kankerbagh
4 NaN Patna – 800 020
5 NaN NaN
6 2 Dr. Deepak Kumar
7 NaN BHR/ 6682/148/2/134095/2007-08/L
8 NaN At & P.o- Bairia
9 NaN P.s- Gourichak
10 NaN Patna – 800 007

最佳答案

尝试:

df = pd.DataFrame(
np.row_stack([df.columns, df.values]),
columns=['id', 'information']
)

关于python - 将 header 添加到 DataFrame Pandas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52479310/

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