使用python解码表的内容
上图是我的工作环境(即熊猫(python)) 我有一个csv文件,我将csv文件的内容传输到python中使用python解码表的内容,python,csv,pandas,Python,Csv,Pandas,上图是我的工作环境(即熊猫(python)) 我有一个csv文件,我将csv文件的内容传输到python中 file_path=filedialog.askopenfilename() csv_file=open(file_path,'r') pd.read_csv(csv_file) 现在,在一组代码之后,我可以在Python中将其内容显示为一个表。 现在我想解码一个特定列“Batch”中的数据 在图中,您可以看到一个表,在该表中,要解码的特定列“Batch”非常重要。 查看**列批次下的数
file_path=filedialog.askopenfilename()
csv_file=open(file_path,'r')
pd.read_csv(csv_file)
现在,在一组代码之后,我可以在Python中将其内容显示为一个表。
现在我想解码一个特定列“Batch”中的数据
在图中,您可以看到一个表,在该表中,要解码的特定列“Batch”非常重要。
查看**列批次下的数据
第一个字符:年份
第二个字符:字母表。它被映射到一个月(a-1月、B-2月、C-3月、D-4月、E-5月……)
第三和第四个字符;日期
Ex:6B08MK1D11的制造日期为2016年2月8日。
现在,我想解码列中的每个数据,根据批次号找到它的日期。解码后,我想创建一个新的列,在其中我将不同日期的值放入一个新列中
比如说
解码此数据“6B08MK1D11”后,我得到的日期为2016年2月8日。现在,对于所有这些单独的批号,我将获得单独的日期,现在应该通过在同一个表中创建一个新列来放置新的日期值
创建新列后,日期列应按a-Z(升序)排序
我试着教如何给Python分配月份:如下所示
for everycode[1] in Bat:
if everycode[1]=='A':
everycode[1] = 'Jan'
if everycode[1]=='B':
everycode[1] = 'Feb'
if everycode[1]=='C':
everycode[1] = 'Mar'
if everycode[1]=='D':
everycode[1]= 'Apr'
if everycode[1]=='E':
everycode[1]= 'May'
if everycode[1]=='F':
everycode[1]= 'Jun'
if everycode[1] == 'G':
everycode[1]= 'Jul'
if everycode[1]=='H':
everycode[1]= 'Aug'
if everycode[1]=='I':
everycode[1]= 'Sep'
if everycode[1]=='J':
everycode[1] = 'Oct'
if everycode[1]=='K':
everycode[1]= 'Nov'
if everycode[1]=='L':
everycode[1]= 'Dec'
但当我执行此操作时,它会返回如下错误:
df = pd.read_csv(csv_file)
df['Batch'] = df['Batch'].apply(interpret_date)
def interpret_date(code):
_year = replace_year(code[0])
_month = replace_month(code[1])
_date = replace_date(code[2:4])
return '-'.join([_date, _month, _year])
“TypeError:'str'对象不支持项分配”您可以尝试以下操作:
df = pd.read_csv(csv_file)
df['Batch'] = df['Batch'].apply(interpret_date)
def interpret_date(code):
_year = replace_year(code[0])
_month = replace_month(code[1])
_date = replace_date(code[2:4])
return '-'.join([_date, _month, _year])
您将需要编写
replace_…()
函数来将每个输入映射到正确的值 在replace_month的例子中,我应该如何教python理解如果是A-一月、B-二月、C-March这样的情况?有很多方法可以做到这一点,一个愚蠢的方法可能是if code[1]='A':返回'Januray'。。。etc
我无法实现。或者获取输出。我不知道如何完成这项任务。