【问题标题】:Using Pandas, How to make each item in a List populate each cell in a pandas column使用熊猫,如何使列表中的每个项目填充熊猫列中的每个单元格
【发布时间】:2017-11-28 18:04:41
【问题描述】:

标题:使用 Pandas,如何使 List 中的每个项目(列表定义为变量)填充列中的每个单元格。

这里很棒的社区,感谢您查看此内容。我研究了很多这个问题,但也许我太业余了。非常感谢任何帮助。

代码:

df = pd.read_excel('OriginalSpreadsheet.xlsx', sheet_name='Sheet1')

list(set(df.COSIGNER))
list(df['COSIGNER'].unique())

x = list(df['COSIGNER'].unique())

df1 = pd.DataFrame()
df1['Unique'] = [x]

twodf = pd.concat([df, df1], ignore_index=True)

输出: - 创建了一个标题为独特的列 - 将列表/数据框 df1 放入标题为“唯一”的列中

问题: - 列表/数据框df1 值全部放入 Excel 电子表格的最后一个单元格中。如何将列表中的每个项目放入我创建的“唯一”列中的单个单元格中。当前和理想的输出在图像中:

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    想出答案:

    df = pd.read_excel('OriginalSpreadsheet.xlsx', sheet_name='Sheet1')
    
    list(set(df.COSIGNER))
    list(df['COSIGNER'].unique())
    
    x = list(df['COSIGNER'].unique())
    

    # ORIGINAL CODE THAT DIDN'T WORK: 
    #df1 = pd.DataFrame()
    #df1['Unique'] = [x]
    df1 = pd.DataFrame(x, columns=['unique'])
    

    # ORIGINAL CODE THAT DIDN'T WORK: 
    #twodf = pd.concat([df, df1], ignore_index=True)
    
    twodf = pd.concat([df, df1], axis=1)
    

    【讨论】:

      猜你喜欢
      • 2020-08-19
      • 1970-01-01
      • 2017-10-31
      • 2015-04-23
      • 2018-08-26
      • 2017-12-18
      • 1970-01-01
      • 2023-01-28
      • 1970-01-01
      相关资源
      最近更新 更多