OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

pandas read_csv set `dtype` by column index (not name)

  • Thread starter Thread starter user189035
  • Start date Start date
U

user189035

Guest
file.txt has a header and four columns. But the headers changes all the time.

something like:

Code:
,'non_standard_header_1','non_standard_header_2','non_standard_header_3'
,kdfjlkjdf, sdfdfd,,
,kdfjlkjwwdf, sdfddffd,,
,kdfjlkjwwdf,, sdfddffd,

I want to import file.txt in pandas, and I want the columns to be import as a object. The intuitive approach (to me):

dtype = [object, object, object] as in:

Code:
    daily_file              = pandas.read_csv('file.txt',
                                              usecols      = [1, 2, 3],
                                              dtype        = [object, object, object])

does not work, running the above, I get:

data type not understood

How to set column dtype on import w/o referencing (existing) column names?
<p><code>file.txt</code> has a header and four columns. But the headers changes all the time. </p>

<p>something like:</p>

<pre><code>,'non_standard_header_1','non_standard_header_2','non_standard_header_3'
,kdfjlkjdf, sdfdfd,,
,kdfjlkjwwdf, sdfddffd,,
,kdfjlkjwwdf,, sdfddffd,
</code></pre>

<p>I want to import <code>file.txt</code> in pandas, and I want the columns to be import as a <code>object</code>. The intuitive approach (to me): </p>

<p><code>dtype = [object, object, object]</code> as in:</p>

<pre><code> daily_file = pandas.read_csv('file.txt',
usecols = [1, 2, 3],
dtype = [object, object, object])
</code></pre>

<p>does not work, running the above, I get:</p>

<p><code>data type not understood</code></p>

<p>How to set column <code>dtype</code> on import w/o referencing (existing) column names?</p>
 

Latest posts

S
Replies
0
Views
1
Sergey Bakaev Rettley
S
Top