Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中,如何将整列数据(行数不固定)复制到同一excel文件中的新列?_Python_Excel_Pandas - Fatal编程技术网

在Python中,如何将整列数据(行数不固定)复制到同一excel文件中的新列?

在Python中,如何将整列数据(行数不固定)复制到同一excel文件中的新列?,python,excel,pandas,Python,Excel,Pandas,我有一个excel文件,其中包含一列“用户名”,我想将该数据复制粘贴到同一工作表中的相邻列中,并称之为“密码”。所有这些都必须在Python程序中完成。您可以尝试: 或者,您可以尝试更简单的解决方案: df = pd.read_excel('testsheet.xlsx') df['password'] = df['username'] df.to_excel("testsheet.xlsx", index=False) 您可以使用pandas.DataFrame.copy。假设这是您的数据

我有一个excel文件,其中包含一列“用户名”,我想将该数据复制粘贴到同一工作表中的相邻列中,并称之为“密码”。所有这些都必须在Python程序中完成。

您可以尝试:

或者,您可以尝试更简单的解决方案:

df = pd.read_excel('testsheet.xlsx')
df['password'] = df['username']
df.to_excel("testsheet.xlsx", index=False) 
您可以使用pandas.DataFrame.copy。假设这是您的数据帧:

    import pandas as pd
    df = pd.read_excel('username.xlsx')
    df
df1 = df.copy()
这将为您提供:

 username
0    a
1    b
2    c
3    d
  username   password
0    a        a
1    b        b
2    c        c
3    d        d
然后创建另一个数据帧:

    import pandas as pd
    df = pd.read_excel('username.xlsx')
    df
df1 = df.copy()
然后在复制内容后,在df中创建一个名为“password”的列,并将其等同于df1

这将为您提供:

 username
0    a
1    b
2    c
3    d
  username   password
0    a        a
1    b        b
2    c        c
3    d        d
然后将其保存到excel:

df.to_excel('username.xlsx' , index = False)

到目前为止,你尝试了什么?请创建一个示例,以所有好的名称阅读,我希望这只是一个实验-你不会真的将所有用户的密码设置为与用户名相同,对吗?也不是根据他们的用户名给他们一个密码?