使用pandas

时间:2015-08-05 10:34:17

标签: python-2.7 pandas

所以,我有一些代码,我试图在python中组合使用电子表格。这是(更新/编辑)代码:

import pandas as pd
import numpy as np
import os
from os.path import basename

df = []

#enter your file names via terminal

file1 = raw_input("Enter the path to the first file(don't forget to include the extension (.xlsx for Windows)):")

file2 = raw_input("Enter the path to the second file(don't forget to include the extension (.xlsx for Windows)):")


#combine the .xlsx files

for f in [file1, file2]:
    data = pd.read_excel(f, 'Sheet1').iloc[:-2]
    data.index = [os.path.basename(f)] * len(data)
    df.append(data)

#add the column that includes the original file

data.index = [basename(f)] * len(data)

#set the path and name of your final product file

final = raw_input('Where do you want the file, and what do you want to name it? Format you answer as such: (C:\path_to_file\name_of_file.xlsx):')

df = pd.concat(df)

df.to_excel(final)

测试输入是:

Product   Inventory Price   Sold
Banana    50        $1.00    27
Grapes    100       $3.00    68

Product        Inventory    Price   Sold
Oranges        68           $3.00   17
Apples         22           $1.50   9
Strawberries   245          $4.00   122

然而,我得到的结果是:

                Product Inventory   Price   Sold
dbtest2.xlsx    Oranges 68             3    17

理想情况下,最终产品将如下所示:

                Product       Inventory Price   Sold
dbtest2.xlsx    Oranges           68    $3       17
dbtest2.xlsx    Apples            22    $1.50    9
dbtest2.xlsx    Strawberries      245   $4.00    122
dbtest1.xlsx    Banana            50    $1.00    27
dbtest1.xlsx    Grapes            100   $3.00    68

它只从第二个列表中取出一个项目......为什么会这样?

0 个答案:

没有答案