使用Pandas将数据编程为CSV

时间:2019-07-03 21:09:15

标签: python python-3.x pandas

我正在尝试制作CSV,Excel,但我遵循了在线帮助,但是它似乎无法正常工作,并且会弹出KeyError:“ Teflon”。有什么想法吗? 这是我所关注的Aid

import pandas as pd
import os

def sort_data_frame_by_Teflon_column(dataframe):
     dataframe = dataframe.sort_values(by= ['Teflon'])
def sort_data_frame_by_LacticAcid_column(dataframe):
    dataframe = dataframe.sort_values(by= ['Lactic Acid'])
def sort_data_frame_by_ExperimentalWeight_column(dataframe):
    dataframe = dataframe.sort_values(by= ['Experimental Weight'])
def sort_data_frame_by_Ratio_column(dataframe):
    dataframe = dataframe.sort_values(by= ['Ratio of Lactic Acid to Experimental Weight'])
def get_data_in_Teflon(dataframe):
    dataframe = dataframe.loc[dataframe['Teflon']]
    dataframe = dataframe.sort_values(by=['Teflon'])
def get_data_in_LacticAcid(dataframe):
   dataframe = dataframe.loc[dataframe['Lactic Acid']]
   dataframe = dataframe.sort_values(by= ['Lactic Acid'])
def get_data_in_ExperimentalWeight(dataframe):
   dataframe = dataframe.loc[dataframe['Experimental Weight']]
   dataframe = dataframe.sort_values(by= ['Experimental Weight'])
def get_data_in_Ratio(dataframe):
   dataframe = dataframe.loc[dataframe['Ratio of Lactic Acid to Experimental Weight']]
   dataframe = dataframe.sort_values(by= ['Ratio of Lactic Acid to Experimental Weight'])
   path = 'C:\\Users\\Light_Wisdom\\Documents\\Spyder\\Mass-TeflonLacticAcidRatio.csv'
 #output_file = open(path,'x')
 #text = input("Input Data: ")
 #text.replace('\\n', '\n')
 #output_file.write(text. replace('\\', ''))
 #output_file.close()
 csv_file = 'C:\\Users\\Light_Wisdom\\Documents\\Spyder\\Mass-TeflonLacticAcidRatio.csv'
 dataframe = pd.read_csv(csv_file)
 dataframe = dataframe.set_index('Teflon')
 sort_data_frame_by_Teflon_column(dataframe)
 sort_data_frame_by_LacticAcid_column(dataframe)
 sort_data_frame_by_ExperimentalWeight_column(dataframe)
 sort_data_frame_by_Ratio_column(dataframe)
 get_data_in_Teflon(dataframe)
 get_data_in_LacticAcid(dataframe)
 get_data_in_ExperimentalWeight(dataframe)
 get_data_in_Ratio(dataframe)
 write_to_csv_file_by_pandas("C:\\images\\Trial1.csv", dataframe)
 write_to_excel_file_by_pandas("C:\\images\\Trial1.xlsx", dataframe)
 #data_frame.to_csv(csv_file_path)
 #excel_writer = pd.ExcelWriter(excel_file_path, engine = 'xlsxwriter')
 #excel_writer.save()

这是CSV:

Teflon,Lactic Acid,Experimental Weight,Ratio of Lactic Acid to Experimental Weight
1.973,.2201,1.56,.14
2.05,.15,.93,.16
1.76,.44,1.56,.28

编辑新问题19年7月24日

我正在尝试使用函数使答案自动化,并且在出现此错误时正在尝试。

def get_Data():
check = 'No'
while(check == 'Yes'):
    row_name = input("What is the row number? ")
    row_name = []
    data = float(input("Teflon, Lactic_Acid, Expt_Wt, LacticAcid_to_Expt1_Wt: "))
    dataframe = []
    check = input("Add another row? ")
    return row_name,data, dataframe
 def row_inputter(row_name,data,dataframe):
    row_name.append(data)
    dataframe.append(row_name)
    return row_name, dataframe

  # Define your data
  #row1 = [ 1.973, .2201, 1.56, .14]
  #row2 = [2.05, .15, .93, .16]
  #row3 = [1.76, .44, 1.56, .28]
 row_name,data, dataframe = get_Data()
 row, df = row_inputter()

2 个答案:

答案 0 :(得分:3)

我可以告诉你你是熊猫的初学者。不用担心...这是您进行前几个操作的方式。

您引用的AID是以老式的方式执行操作的,没有利用已经创建的许多优秀工具来处理熊猫和Python中的CSV和XLSX数据。

XLSXWriter是一个神话般的库,可以轻松读取和写入Pandas数据。
[XLXSwriter.com] [1] https://xlsxwriter.readthedocs.io/working_with_pandas.html

# Do necessary imports
import pandas as pd
import os
import xlsxwriter

# Define your data
expt_data = ["Teflon", "Lactic_Acid", "Expt_Wt", "LacticAcid_to_Exptl_Wt"]
row1 = [ 1.973, .2201, 1.56, .14]
row2 = [2.05, .15, .93, .16]
row3 = [1.76, .44, 1.56, .28]

# Create dataframe using constructor method
df1 = pd.DataFrame([row1, row2, row3], columns=expt_data)

# Output dataframe
df1

# Sort dataframe by Teflon column values and output it
Teflon_Sorted = df1.sort_values(by=["Teflon"])
Teflon_Sorted

# Sort dataframe by Lactic_Acid column values and output it
Lactic_Acid_Sorted = df1.sort_values(by=["Lactic_Acid"])
Lactic_Acid_Sorted

# Sort dataframe by Expt_Wt column values and output it
Expt_Wt_sorted = df1.sort_values(by=["Expt_Wt"])
Expt_Wt_sorted

# Sort dataframe by Expt_Wt column values and output it
LacticAcid_to_Exptl_Wt_sorted = df1.sort_values(by=["LacticAcid_to_Exptl_Wt"])
LacticAcid_to_Exptl_Wt_sorted

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter("Trial1.xlsx", engine='xlsxwriter')

# Convert all dataframes to XlsxWriter Excel objects and then write each to a different worksheet in the workbook created above named "Trial1.xlsx".
Teflon_Sorted.to_excel(writer, sheet_name='Teflon_Sorted')
Lactic_Acid_Sorted.to_excel(writer, sheet_name='Lactic_Acid_Sorted')
Expt_Wt_sorted.to_excel(writer, sheet_name='Expt_Wt_sorted')
LacticAcid_to_Exptl_Wt_sorted.to_excel(writer, sheet_name='LacticAcid_to_Exptl_Wt_sorted')

# Close the Pandas Excel writer and output the Excel file.
writer.save()

# now go to your current directory in your file system where the Jupyter Notebook or Python file is executing and find your file. 

# Type !dir in Jupyter cell to list current directory on MS-Windows
!dir 

[XLXSwriter.com] [1] https://xlsxwriter.readthedocs.io/working_with_pandas.html

抱歉,这不是一个完整的应用程序,可以完成您想要的一切,但是我的时间有限。我向您展示了如何写出最终结果。我把它留作学习练习,让您学习如何读取数据文件,而不是在Python程序中内联“即时”创建它。

我的建议是将XLXSwriter用于与Excel或Pandas相关的所有内容。遵循XLSXwriter网站上的精彩教程。 XLSXwriter可能是目前最好,最简单的Python-Pandas-Excel工具箱。它会以编程方式完成某人通常必须手动(“交互式”)进行的所有操作。

答案 1 :(得分:2)

您已经通过

Teflon设置为索引
 dataframe = dataframe.set_index('Teflon')

dataframe不再包含该列。您的功能

sort_data_frame_by_Teflon_column()

将失败并通过该错误。

此外,其他功能如下:

def get_data_in_LacticAcid(dataframe):
   dataframe = dataframe.loc[dataframe['Lactic Acid']]
   dataframe = dataframe.sort_values(by= ['Lactic Acid'])

很可能会失败或由于第一行而将您的dataframe变为空。您到底想用这些功能实现什么?