Kingindanord
Kingindanord

Reputation: 2036

Assign data type for each column in pandas DataFrame - Python

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

Answers (3)

Sureya Pragaash
Sureya Pragaash

Reputation: 31

I have also faced this problem initially but I have found a solution:

  1. Convert the data frame column to a list data structure in Python.
  2. Then convert the list to a series after import numpy package.
  3. Using the 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

piRSquared
piRSquared

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

ShreyasG
ShreyasG

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

Related Questions