动态将数据导入熊猫数据框

时间:2019-09-26 16:15:35

标签: python python-3.x pandas

我正在尝试让python要求用户打开文件位置,从该位置他们希望将数据读取到数据框中。

我正在尝试的代码是:

import datetime
import pandas as pd
import json
import gc
import os
from pandas.io.json import json_normalize

Today = datetime.datetime.today()                                               # Set Today string to today's name
Today_day = Today.strftime("d")                                                 # Set Today_day to the date in the month (e.g. 21)

while True:
    user_input = input("Enter the path of your file (Remember to include the file name and extension): ")       # Asks user for filepath of file to be used

    if not os.path.isfile(user_input):                                                                          # If filepath is not correct (e.g. missing or incorrect filename)
        print()                                                                                                 # Print a blank line
        print("I did not find the file at "+str(user_input))                                                    # Print that file was not found
        print()                                                                                                 # Print a blank line
        continue                                                                                                # Goes back to asking for a correct filepath

    else:                                                                                                       # If filepath is correct
        break                                                                                                   # Break out of while loop

#iFile = open(user_input,'rt')                                                                                   # Opens file, if present, in read only mode ("r"), specifying that it is a text file ("t")

gc.collect()                                                                    # Empty garbage collect
SIEBEL = pd.read_csv(user_input, encoding = "ISO-8859-1")                       # Get data from SIEBEL PSD2 extract
SIEBEL.fillna("", inplace = True)                                               # Replace null values with blanks
SIEBEL.sort_values("CIF", inplace=True)                                         # Sort dataframe by CIF values

print ()                                                                        # Print blank row
print (SIEBEL.dtypes)

print ()                                                                        # Print blank row
print ("SIEBEL extract contains:", len(SIEBEL.index), "rows")                   # Print number of rows in SIEBEL data
print ("SIEBEL extract contains:", len(SIEBEL.columns), "columns")              # Print number of columns in SIEBEL data

SIEBEL.drop(columns=["Eircode"], axis=1, inplace=True)                          # Drops oa column, in this case Eircode from the dataframe

print ()                                                                        # Print blank row
print ("SIEBEL extract contains:", len(SIEBEL.columns), "columns")              # Print number of rows in SIEBEL data

print ()
print (SIEBEL.dtypes)                                                           # Print all types in dataframe
SIEBEL.to_csv(r"C:\Users\LIDJ622\Desktop\SIEBEL_export"+Today.strftime("%B %#d")+".csv", index=None, header=True)   # Save the sorted file as a CSV to the desktop

但是,当我尝试此操作时,我会得到

  

runfile('C:/ Users / LIDJ622 / Desktop / Test 1增量.py',   wdir ='C:/ Users / LIDJ622 / Desktop')

     

输入文件的路径(请记住包括文件名和   扩展程序):SIEBEL Extract 20k v1.csv Traceback(最近通话)   最后):

     

文件“”,第1行,在       runfile('C:/ Users / LIDJ622 / Desktop / Test 1增量.py',wdir ='C:/ Users / LIDJ622 / Desktop')

     

文件   “ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ spyder \ utils \ site \ sitecustomize.py”,   运行文件中的第705行       execfile(文件名,命名空间)

     

文件   “ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ spyder \ utils \ site \ sitecustomize.py”,   第102行,在execfile中       exec(compile(f.read(),文件名,'exec'),命名空间)

     

文件“ C:/ Users / LIDJ622 / Desktop / Test 1增量.py”,第35行,在          SIEBEL.sort_values(“ CIF”,inplace = True)#按CIF值对数据框进行排序

     

文件   “ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ pandas \ core \ frame.py”,   第3634行,在sort_values中       na_position = na_position)

     

文件   “ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ pandas \ core \ sorting.py”,   第250行,在nargsort中       索引器= non_nan_idx [non_nans.argsort(kind = kind)]

     

TypeError:“ str”和“ float”的实例之间不支持“ <”

如何让Python询问用户文件的位置,然后将其导入数据框?

编辑:包括完整的代码和错误消息

0 个答案:

没有答案