0

I want to manipulate the dataframe element into the data type of the element. the example below dataframe.

A      B         C           D
1      2       12-03-2010   1
45     acxz    3-02-2010    3
43     2.9     14-08-2010   4
45.3   3.5     23-09-2019   2.46
nan    34.23   09-09-2020   09-02-2015
45     nan     sam          10-09-2016
24     45.23   02-03-2021   wax
xyz    32      raz          na

Transform this dataframe into below dataframe.

A        B         C         D
int     int     datetime    int
int     string  datetime    int
int     float   datetime    int
float   float   datetime    float
string  float   datetime    datetime
int     string  string      datetime
int     float   datetime    string
string  int     string      string

can anyone suggest me any way to achieve the below dataframe from the above data using pandas.

2
  • @jezrael but head only extract at max 5 data without argument and below one is new dataframe from the above dataframe. Commented Feb 2, 2021 at 7:14
  • so, since head is the issue in the example code from jezrael, all you need to do is.... Commented Feb 2, 2021 at 7:14

1 Answer 1

1

Use DataFrame.applymap by types, convert to strings and last use replace:

df = pd.DataFrame({
    "a": [1, 0.01, 'aaa', pd.to_datetime('2015-01-01')],
    "b": ['yy', pd.to_datetime('2015-01-01'), 1, 0]
   
})

d = {"<class 'pandas._libs.tslibs.timestamps.Timestamp'>":'datetime',
     "<class 'int'>":'int',
     "<class 'float'>":'float',
     "<class 'str'>":'string'}
df = df.applymap(type).astype(str).replace(d)
print (df)
          a         b
0       int    string
1     float  datetime
2    string       int
3  datetime       int
Sign up to request clarification or add additional context in comments.

8 Comments

how you are generating d variable dictonary.
@Rishav - Manually, it is only for replace
great @Jezrael.
@jezreal yes, but need to check for Nan values.
@jezreal getting issue with DateTime, the above script treats it as a string, not as DateTime.
|

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.