Все значения NAN при фильтрации панд DataFrame в один столбец - PullRequest
0 голосов
/ 03 октября 2019

Я импортирую данные из файла .csv , который хранится в одном кадре данных. Там все выглядит хорошо: enter image description here

После чего я пытаюсь хранить только один столбец кадра данных в другом месте. Однако он возвращает все значения NaN:

enter image description here

Точно такой же код отлично работает для файла .xls ранее в том же скрипте Python. Поэтому я не уверен, что здесь происходит. Любое разъяснение будет оценено. Вот исходный код:

    # ------------------------------------------------------------------------------
print("\nSELECT Q MEASUREMENT FILE TO FIX: ")
time.sleep(1)
# Allow User to pick file that which needs X-Y data to be FIXED
tkinter.Tk().withdraw()  # Close the root window
input2 = filedialog.askopenfilename()
print("\nYou selected file:")
print(input2)
print("\n")
input2 = str(input2)

# Check to see if directory/file exists
assert os.path.exists(input2), "File does not exist at, "+str(input2)

# Import data below and store in df
print("\nImporting Excel Workbook...")
time.sleep(1)
# You can check encoding of file with notepad++
dfQ = pd.read_csv(input2, encoding="ansi")
dfQ.values
print(dfQ)  # This DataFrame (dfQ) contains the entire excel workbook
print("\n\nWorkbook Successfully Imported")
time.sleep(.5)
print("...")

# Search Q measurements CSV for "Chip ID" and matches it to corresponding
# "PartID" in the master table created from manually fixed file.
print("Matching PartID's to update proper X-Y values")
time.sleep(.5)
print("...")
IDs = pd.DataFrame(dfQ, columns=['Chip ID'])
time.sleep(.5)
print(IDs)
s = IDs.size
print("\nSuccessfully extracted", s, "Chip ID's!")
print(dfQ.columns)

Ответы [ 2 ]

2 голосов
/ 03 октября 2019

Проблема в том, что ваш столбец на самом деле называется Chip ID (с пробелом), а не Chip ID.

Так что либо IDs = dfQ[" Chip ID"] для серии, либо IDs = dfQ[[" Chip ID"]] должны работать оба.

2 голосов
/ 03 октября 2019

Все, что вам нужно сделать, это:

IDs = dfQ["Chip ID"]

, и вы получите соответствующий pandas.series. Если вы хотите, чтобы результат в pandas.DataFrame форматировался, просто выполните:

IDs = dfQ["Chip ID"].to_frame()

РЕДАКТИРОВАТЬ:

Имя столбца начинается с пробела:

Index(['Date', ' Time', ' Device ID', ' Chip ID', ' Lot', ' Wafer', ' X', ' Y',
       ' Q half-width', ' Q fit', ' dQ¸ %', ' Internal Resonant F',
       ' Internal Resonant A', ' Ajusted FG Ampl', ' FG Amplitude (0.10)',
       ' Forced A', ' Forced F', ' Drive Gain', ' Frequency sweep¸start',
       ' Prelim Q  half-width', ' Prelim Q fit', ' Prelim Q Error¸ %',
       ' Execution time', ' Preliminary F', ' residue', ' '],
      dtype='object')

так что все, что вам нужно сделать:

IDs = dfQ[" Chip ID"]
...