I have data that contains the following string, and I want to make the first column an integer, but it doesn't work and I get an error.
Why is that?
I'm not sure.
The data is as follows.
['id_NO', 'PROC_YM', 'CL_START', 'CL_STOP',
['000001', '201912', '2019/12/1', '2019/12/31'],
['000022', '201912', '2019/12/1', '2019/12/31'],
['000333', '201912', '2019/12/1', '2019/12/31'],
['004444', '201912', '2019/12/1', '2019/12/31'],
['055555', '201912', '2019/12/1', '2019/12/31'],
['666666', '201912', '2019/12/1', '2019/12/31'],
['020000', '201912', '2019/12/1', '2019/12/31'],
['003300', '201912', '2019/12/1', '2019/12/31'],
['040044', '201912', '2019/12/1', '2019/12/31']]
Script
date['id_NO']=date['id_NO'].astype(int)
print(date)
Error Contents
TypeError:list indications must be integrators or slices, not str
The .astype()
in the question is a method of numpy.ndarray
and pandas.Series
, pandas.DataFrame
, and cannot be used for the normal list.
This data seems to be table data, so I think it would be better to convert it to pandas.DataFrame
and then use the method of the question.
import pandas as pd
data=['id_NO', 'PROC_YM', 'CL_START', 'CL_STOP',
['000001', '201912', '2019/12/1', '2019/12/31'],
['000022', '201912', '2019/12/1', '2019/12/31'],
['000333', '201912', '2019/12/1', '2019/12/31'],
['004444', '201912', '2019/12/1', '2019/12/31'],
['055555', '201912', '2019/12/1', '2019/12/31'],
['666666', '201912', '2019/12/1', '2019/12/31'],
['020000', '201912', '2019/12/1', '2019/12/31'],
['003300', '201912', '2019/12/1', '2019/12/31'],
['040044', '201912', '2019/12/1', '2019/12/31']]
df=pd.DataFrame (data[1:], columns=data[0])
df['id_NO'] = df['id_NO'].astype(int)
print(df)
# id_NO PROC_YMCL_STARTCL_STOP
#0 1 201912 2019/12/1 2019/12/31
#1 22 201912 2019/12/1 2019/12/31
#2 333 201912 2019/12/1 2019/12/31
#3 4444 201912 2019/12/1 2019/12/31
#4 55555 201912 2019/12/1 2019/12/31
#5 666666 201912 2019/12/1 2019/12/31
#6 20000 201912 2019/12/1 2019/12/31
#7 3300 201912 2019/12/1 2019/12/31
#8 40044 201912 2019/12/1 2019/12/31
date[1][0]
.Alternatively, you can convert this list to pandas DataFrame and so on before working.'id_NO'
as the name of the column, but simply appears to have a column name on the first line.You must set the column name appropriately.
© 2025 OneMinuteCode. All rights reserved.