Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8666533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:45:24+00:00 2026-06-12T17:45:24+00:00

If read a file with default column names, how do call them after? df[1]

  • 0

If read a file with default column names, how do call them after?
df[1] seems to work almost all of the time. However, it complains about types when writing conditions like:

In [60]: cond = ((df[1] != node) & (df[2] != deco))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/home/ferreirafm/work/colab/SNP/rawdata/<ipython-input-60-513a433bfeb5> in <module>()
----> 1 cond = ((df[1] != node) & (df[2] != deco))

/usr/lib64/python2.7/site-packages/pandas/core/series.pyc in wrapper(self, other)
140             if np.isscalar(res):
141                 raise TypeError('Could not compare %s type with Series'
--> 142                                 % type(other))
143             return Series(na_op(values, other),
144                           index=self.index, name=self.name)

TypeError: Could not compare <type 'str'> type with Series

Treat dataframe columns by default names are more appropriate for my applications.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:45:25+00:00Added an answer on June 12, 2026 at 5:45 pm

    It seems that you compare a series of scalar values to a string:

    In [73]: node = 'a'
    
    In [74]: deco = 'b'
    
    In [75]: data = [(10, 'a', 1), (11, 'b', 2), (12, 'c', 3)]
    
    In [76]: df = pd.DataFrame(data)
    
    In [77]: df
    Out[77]: 
        0  1  2
    0  10  a  1
    1  11  b  2
    2  12  c  3
    
    In [78]: cond = ((df[1] != node) & (df[2] != deco))
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-78-0afad3702859> in <module>()
    ----> 1 cond = ((df[1] != node) & (df[2] != deco))
    
    /home/.../python2.7/site-packages/pandas/core/series.pyc in wrapper(self, other)
        140             if np.isscalar(res):
        141                 raise TypeError('Could not compare %s type with Series'
    --> 142                                 % type(other))
        143             return Series(na_op(values, other),
        144                           index=self.index, name=self.name)
    
    TypeError: Could not compare <type 'str'> type with Series
    

    Note that pandas can handle strings and numbers in a series, but it not really makes sense to compare strings and numbers, so the error message is useful.
    However pandas should perhaps give a more detailed error message.

    If your condition for the column 2 would be a number it would work:

    In [79]: deco = 3
    
    In [80]: cond = ((df[1] != node) & (df[2] != deco))
    
    In [81]: df[cond]
    Out[81]: 
        0  1  2
    1  11  b  2
    

    Some comments:

    Maybe some of your confusion is due to a design decision in pandas:

    If you read data from a file with read_csv the default column names of the resulting data frame are set to X.1 to X.N (and to X1 to XN for versions >= 0.9), which are strings.

    If you create a data frame from exiting arrays or lists or something the column names default to 0 to N and are integers.

    In [23]: df = pd.read_csv(StringIO(data), header=None)
    
    In [24]: df.columns
    Out[24]: Index([X.1, X.2, X.3], dtype=object)
    
    In [25]: df.columns[0]
    Out[25]: 'X.1'
    
    In [26]: type(df.columns[0])
    Out[26]: str
    
    In [27]: df = pd.DataFrame(randn(2,3))
    
    In [30]: df.columns
    Out[30]: Int64Index([0, 1, 2])
    
    In [31]: df.columns[0]
    Out[31]: 0
    
    In [32]: type(df.columns[0])
    Out[32]: numpy.int64
    

    I opened a ticket to discuss this.

    So your

    In [60]: cond = ((df[1] != node) & (df[2] != deco))
    

    should work for a dataframe created from an array or something, if the type of df[1] and df[2] is the same as the type of node and deco.

    If you have read a file with read_csv than

    In [60]: cond = ((df['X.2'] != node) & (df['X.3'] != deco))
    

    should work with versions < 0.9, while it should be

    In [60]: cond = ((df['X2'] != node) & (df['X3'] != deco))
    

    with versions >= 0.9.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say that I call select() on a FD_SET containing a bunch of read file
I read file, but in the end of file i get unknown symbols: int
I am trying to read file headers using java, I want to get file
if I read file by calling read() like this: unsigned char buf[512]; memset(buf, 0,
I try to read file /proc/'pid'/status, using c program. The code is as follows,
I am trying to read file and split its line to get some context(Computer
When I using the following code to read file: lines=file(data.txt).read().split(\n) I have the following
I am using csv.reader to read file but it giving me whole file. file_data
I'm having problems reading this one JPEG file using ImageIO.read(File file) - it throws
Three days i was trying to figure out how to read file using relative

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.