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 8608327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:35:03+00:00 2026-06-12T03:35:03+00:00

I have the following indexed DataFrame with named columns and rows not- continuous numbers:

  • 0

I have the following indexed DataFrame with named columns and rows not- continuous numbers:

          a         b         c         d
2  0.671399  0.101208 -0.181532  0.241273
3  0.446172 -0.243316  0.051767  1.577318
5  0.614758  0.075793 -0.451460 -0.012493

I would like to add a new column, 'e', to the existing data frame and do not want to change anything in the data frame (i.e., the new column always has the same length as the DataFrame).

0   -0.335485
1   -1.166658
2   -0.385571
dtype: float64

How can I add column e to the above example?

  • 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-12T03:35:05+00:00Added an answer on June 12, 2026 at 3:35 am

    Edit 2017

    As indicated in the comments and by @Alexander, currently the best method to add the values of a Series as a new column of a DataFrame could be using assign:

    df1 = df1.assign(e=pd.Series(np.random.randn(sLength)).values)
    

    Edit 2015
    Some reported getting the SettingWithCopyWarning with this code.
    However, the code still runs perfectly with the current pandas version 0.16.1.

    >>> sLength = len(df1['a'])
    >>> df1
              a         b         c         d
    6 -0.269221 -0.026476  0.997517  1.294385
    8  0.917438  0.847941  0.034235 -0.448948
    
    >>> df1['e'] = pd.Series(np.random.randn(sLength), index=df1.index)
    >>> df1
              a         b         c         d         e
    6 -0.269221 -0.026476  0.997517  1.294385  1.757167
    8  0.917438  0.847941  0.034235 -0.448948  2.228131
    
    >>> pd.version.short_version
    '0.16.1'
    

    The SettingWithCopyWarning aims to inform of a possibly invalid assignment on a copy of the Dataframe. It doesn’t necessarily say you did it wrong (it can trigger false positives) but from 0.13.0 it let you know there are more adequate methods for the same purpose. Then, if you get the warning, just follow its advise: Try using .loc[row_index,col_indexer] = value instead

    >>> df1.loc[:,'f'] = pd.Series(np.random.randn(sLength), index=df1.index)
    >>> df1
              a         b         c         d         e         f
    6 -0.269221 -0.026476  0.997517  1.294385  1.757167 -0.050927
    8  0.917438  0.847941  0.034235 -0.448948  2.228131  0.006109
    >>> 
    

    In fact, this is currently the more efficient method as described in pandas docs


    Original answer:

    Use the original df1 indexes to create the series:

    df1['e'] = pd.Series(np.random.randn(sLength), index=df1.index)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following div in a page (I can not modify). <div id=:0.control>Click me</div>
I have the following indexed view: ALTER View [CIC].[vwCoaterC473Heat] WITH SCHEMABINDING AS Select id
I have the following array, named $usergrouppermissions: Array ( [0] => Array ( [UserGroupPermission]
I have a table (MyTable) with the following columns: Col1: NameID VARCHAR(50) PRIMARY KEY
I am using a DBD::SQLite in memory database. I have defined the following indexes
I am fairly new to Indexes. I have table following table [FORUM1] [msg_id] [int]
I have following plist: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE plist PUBLIC -//Apple//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd>
I have following source. In insertMessage(..), it calls selectMessage to check whether duplicate record
I have following fiddle: http://jsfiddle.net/BFSH4/ As you see there are two issues: The h1
I have following script that executes all the .reg files in the current directory.

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.