python- get column dataType from a dataframe


python- get column dataType from a dataframe



I am trying to get a column data type from a dataframe



Here is a sample code:


print training_data.schema
print 'fields'
print training_data.schema.fields
print 'names'
print training_data.schema.names



The above code prints as shown below:
StructType(List(StructField(id,LongType,true),StructField(text,StringType,true),StructField(label,DoubleType,true)))
fields
[StructField(id,LongType,true), StructField(text,StringType,true), StructField(label,DoubleType,true)]
names
['id', 'text', 'label']



But how can I get the datatype for label column ? Thanks a lot for your time.



Regards





check this,stackoverflow.com/questions/37335307/…
– Suresh
Jul 2 at 6:29




2 Answers
2


df['col label'].dtype



Is one option.



Edit


name_dtype = df['col label'].dtype.name





I tried this, but it prints: Column<label['dtype']>
– Pratap D
Jul 2 at 5:05





Try my edit perhaps.
– David
Jul 2 at 5:08





tried, it prints <bound method Column.alias of Column<label['dtype']>>
– Pratap D
Jul 2 at 5:10





Oops, I think we might be talking about different dataframes. My answer was with regards to Pandas dataframes and not SQL dataframes. My bad. If you have Pandas installed, you could always convert your SQL dataframe to a Pandas dataframe and try my solution.
– David
Jul 2 at 5:13





oh,ok I will try that. Sorry I am new to this python. What is the difference between SQL dataframe and pandas dataframe?
– Pratap D
Jul 2 at 5:16



Thanks for all the responses.
I found the below solution, hoping it will be helpful to any one looking for the answer:


for f, v in zip(df.schema.fields, df.schema.names):
if v == colname:
datatype = f.dataType






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to add background colour in existing image using Swift?

Moria Casán

How to make file upload 'Required' in Contact Form 7?