5

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)
3
  • In your minimal sample, you specify some list of columns and then the resultant df doesn't show these columns, also why does this matter for an empty df? The dtype will be changed once you assign data to those columns Commented Aug 31, 2017 at 10:44
  • I think only is possible set same dtype in all columns by constructor like df = pd.DataFrame(columns=columns, dtype=str) Commented Aug 31, 2017 at 10:46
  • yes, it will work. The documentation describes it so df = pd.DataFrame(columns=columns, dtype=str) will give df.dtypes as objects Commented Aug 31, 2017 at 10:50

3 Answers 3

6

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
Sign up to request clarification or add additional context in comments.

1 Comment

If you have any float NaN, using astype(str) will convert them the values to "nan"
0

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)

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.