1

I have dataframe df with following value:

0                                    abcc
1                                      11
2                                    TRUE
3                                 "123.5"
4                            192.168.1.55
5                   "123.4555, 123.53422"
6                              12/23/1999
7                                      AF
8                        9° 3' 33.228'' N
9                      9° 47' 20.6268'' W
10  "8° 3' 33.228'' N,8° 47' 20.6268'' W"
11                             1582088645

I transposed the data frame and checked the dtypes are shown for sequence number of axis:

data=data.transpose()
    for i, j in data.dtypes.iteritems():
        print(i,j)
 0 object
 1 object
 .
 .
 .
 11 object

How to skip this sequence number . so i can achive dtype of each value? I want something like this

abcc object
10 int64
TRUE bool
“123.5” object
192.168.1.54 object
“123.4555, 123.53422” object
12/23/1998 object
AF object
8° 3' 33.228'' N object
8° 47' 20.6268'' W object
“8° 3' 33.228'' N,8° 47' 20.6268'' W” object
1582088644 int64
mujjiga
  • 16,186
  • 2
  • 33
  • 51
Suhas Kashyap
  • 398
  • 3
  • 14
  • Just do this: `data.dtypes` Will return all column names with dtypes – Raghul Raj Mar 09 '20 at 06:50
  • @RaghulRaj He has used data.dtypes only. – Pygirl Mar 09 '20 at 06:52
  • data.dtypes dosent work it gives me the dtype of indices – Suhas Kashyap Mar 09 '20 at 06:53
  • 1
    Your initial dataframe (before the transpose) `data` contains only one column. So pandas has converted all the values to `object`. Take a look at [Essential Basic Functionality](https://pandas.pydata.org/pandas-docs/stable/getting_started/basics.html#basics-dtypes) to set `data types` for indiviudal columns – DOOM Mar 09 '20 at 07:00
  • does this helps ? https://stackoverflow.com/q/49926897/6660373 – Pygirl Mar 09 '20 at 07:06
  • @DOOM i am reading list and constructing a dataframe which has no headers, since i am unaware of what the list will be i cant set the data types, even before transposing – Suhas Kashyap Mar 09 '20 at 07:20
  • @Pygirl it converts everything to string! so i cant get dtype of dataframe – Suhas Kashyap Mar 09 '20 at 07:21
  • If I am correct pandas can't have mixed data type in a column. If there is int, boolean then they will be treated as an object. – Pygirl Mar 09 '20 at 09:57

1 Answers1

0

Its a little bit unconventional but gives you, what you want:

df =pd.DataFrame(data=["abcc", 11, "TRUE", "123.5", "192.168.1.55", "123.4555, 123.53422",
                       "12/23/1999","AF","9° 3' 33.228'' N", "9° 47' 20.6268'' W", 
                       "8° 3' 33.228'' N,8° 47' 20.6268'' W", 1582088645])
for col, dtype in zip(df.T.values[0],df.T.dtypes):
    print(col, dtype)

Output is:

abcc object
11 object
TRUE object
123.5 object
192.168.1.55 object
123.4555, 123.53422 object
12/23/1999 object
AF object
9° 3' 33.228'' N object
9° 47' 20.6268'' W object
8° 3' 33.228'' N,8° 47' 20.6268'' W object
1582088645 object

You have to be careful, because this will work only with the data you provided

coco18
  • 836
  • 8
  • 18