程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了将数据从 Excel sheet1 映射到 sheet2 在 python 中创建重复列大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决将数据从 Excel sheet1 映射到 sheet2 在 python 中创建重复列?

开发过程中遇到将数据从 Excel sheet1 映射到 sheet2 在 python 中创建重复列的问题如何解决?下面主要结合日常开发的经验,给出你关于将数据从 Excel sheet1 映射到 sheet2 在 python 中创建重复列的解决方法建议,希望对你解决将数据从 Excel sheet1 映射到 sheet2 在 python 中创建重复列有所启发或帮助;

我正在尝试使用 Pandas 数据框将值从 Sheet1 映射到 sheet2,列名列在 sheet2 中,但是当我将数据写入工作表 2 时,它使第一列为空并将数据附加到重复的列中。

>

当我尝试打印 dataframe2 时也会发生这种情况

df1 = pd.read_excel(open(r'C:\Users\Desktop\notepad.xLSX','rb'),sheet_name='sheet1')
df2 = pd.read_excel(open(r'C:\Users\Desktop\notepad.xLSX',sheet_name='sheet2')


df2['col1'] = df1['col3']
df2['col2'] = df1['col5']
df2['col3'] = df1['col8']
df2['col4'] = df1['col6']

当尝试打印 df1 时,它给出了正确的数据 但是当尝试打印 df2 列时,第一列有空值重复

print(df2)

Output

col1 col2 col3 col4  col1 col2 col3 col4
NaN  NaN  NaN  NaN   Ind  Aus  Nz   Aus
NaN  NaN  NaN  NaN   Sri  Afg  Chi  Ber
NaN  NaN  NaN  NaN   Usa  Uk   Un   Ind

如何避免具有空值的重复列

解决方法

为了避免列名称中包含任何非标准字符或列名称出现任何其他奇怪问题的潜在问题,您是否尝试过:

df2[0] = df1['col3']
df2[1] = df1['col5']
df2[2] = df1['col8']
df2[3] = df1['col6']

另外,请问print(df1)的输出是什么?

大佬总结

以上是大佬教程为你收集整理的将数据从 Excel sheet1 映射到 sheet2 在 python 中创建重复列全部内容,希望文章能够帮你解决将数据从 Excel sheet1 映射到 sheet2 在 python 中创建重复列所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。