I am trying to change the column TOPIxd data to String type with the code below.
If you check the data type in data.dtypes
, it will remain the object type as it was originally.
I wonder why...
import pandas as pd
import numpy as np
df = pd.read_csv("forcasting topx.csv", header = 0)
# Delete the opening few lines
data = df.drop ([0,1,2,3])
#Modification of type
data['TOPIX d'].astype(np.str)
data.dtypes
The numpy
and pandas
treat strings differently. numpy
has a fixed length of np.str
in an array, but pandas
has only a pointer to an object in DataFrame.
I don't know why you're trying to change the data to String type, but if it's an element that originally becomes a string, you don't have to worry about it because each element of DataFrame is of type str
.
Also, if an element contains a number and you want to change it to a string, you can convert it with astype(str)
. If you look at dtypes
, it remains the object
type, but if you look at each element type, it is the str
type.
data=pd.DataFrame([['a'], ['b'], [3]])
>>>data=pd.DataFrame([['a'],['b'],[3]])
>> type (data.iloc [2,0])
<class 'int'>
>>>data[0]=data[0].astype(str)
>> type (data.iloc [2,0])
<class'str'>
pandas.DataFrame.astype
simply returns what you cast and is not a function that changes the original dataframe content.Here is a sample code for this:
>>ser=pd.Series ([1,2], dtype='int32')
>>ser.astype('int64')
0 1
1 2
dtype —int64
>>ser
0 1
1 2
dtype —int32
If you want to treat it as a string value, you can substitute the result of astype
or specify the dtype from the beginning using the optional argument dtype
in read_csv
.
© 2024 OneMinuteCode. All rights reserved.