Reputation: 2036
I want to change the data type that could be entered in an empty Pandas data frame. So I've tried the following approach but it doesn't work. can someone please give me a hint how to solve this issue?
columns=['A', 'B', 'C', 'D', 'E','F']
df = pd.DataFrame(columns=columns)
>>> df
Empty DataFrame
Columns: [A, B, C, D, E, F]
Index: []
df[[0]]=df[[0]].astype(str)
df[[1]]=df[[1]].astype(str)
df[[2]]=df[[2]].astype(int)
df.iloc[:,3:6]=df.iloc[:,3:6].astype(float)
Upvotes: 5
Views: 9926
Reputation: 31
I have also faced this problem initially but I have found a solution:
astype()
function convert to the desired data type. Code:
list = list(data['unknown'])
series = pd.Series(list)
seriesOfTypeBool = g.astype(np.bool)
data['unknown'] = seriesOfTypeBool` <br>
And Simplified Version:
data['Action'] = pd.Series(list(data['Action'])).astype(np.bool)
Upvotes: 0
Reputation: 294506
You want to construct a series or dictionary with your desired types then use astype
columns = list('ABCDEF')
df = pd.DataFrame(columns=columns)
dtypes = {k: str for k in columns[:2]}
dtypes.update({columns[2]: int})
dtypes.update({k: float for k in columns[3:]})
df = df.astype(dtypes)
df.dtypes
A object
B object
C int64
D float64
E float64
F float64
dtype: object
Upvotes: 6
Reputation: 806
By default, all your columns will be of 'object' type so you might not need to force columns to be of type string as such. For the other column types, something like this might work?
df[['C']] = df[['C']].apply(pd.to_numeric)
Upvotes: 0