Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add indexed column to DataFrame with pandas

Tags:

python

pandas

I'm a beginning pandas user, and after studying the documentation I still can't find a straightforward way to do the following.

I have a DataFrame with a pandas.DateRange index, and I want to add a column with values for part of the same DateRange.

Suppose I have

df

                            A         B
2010-01-01 00:00:00  0.340717  0.702432
2010-01-01 01:00:00  0.649970  0.411799
2010-01-01 02:00:00  0.932367  0.108047
2010-01-01 03:00:00  0.051942  0.526318
2010-01-01 04:00:00  0.518301  0.057809
2010-01-01 05:00:00  0.779988  0.756221
2010-01-01 06:00:00  0.597444  0.312495

and

df2

                     C
2010-01-01 03:00:00  5
2010-01-01 04:00:00  5
2010-01-01 05:00:00  5

How can I obtain something like this:

                            A         B    C
2010-01-01 00:00:00  0.340717  0.702432    nan
2010-01-01 01:00:00  0.649970  0.411799    nan
2010-01-01 02:00:00  0.932367  0.108047    nan
2010-01-01 03:00:00  0.051942  0.526318    5
2010-01-01 04:00:00  0.518301  0.057809    5
2010-01-01 05:00:00  0.779988  0.756221    5
2010-01-01 06:00:00  0.597444  0.312495    nan
like image 371
saroele Avatar asked Mar 18 '12 22:03

saroele


People also ask

How will you add a column at a specific index to a pandas DataFrame?

Answer. Yes, you can add a new column in a specified position into a dataframe, by specifying an index and using the insert() function. By default, adding a column will always add it as the last column of a dataframe. This will insert the column at index 2, and fill it with the data provided by data .

How do I add an index to a DataFrame?

DataFrame - set_index() function The set_index() function is used to set the DataFrame index using existing columns. Set the DataFrame index (row labels) using one or more existing columns or arrays of the correct length. The index can replace the existing index or expand on it.

How do I add a column to a pandas DataFrame?

In pandas you can add/append a new column to the existing DataFrame using DataFrame. insert() method, this method updates the existing DataFrame with a new column. DataFrame. assign() is also used to insert a new column however, this method returns a new Dataframe after adding a new column.

How do you set a column as index?

To set a column as index for a DataFrame, use DataFrame. set_index() function, with the column name passed as argument. You can also setup MultiIndex with multiple columns in the index. In this case, pass the array of column names required for index, to set_index() method.


1 Answers

Do df.join(df2):

http://pandas.pydata.org/pandas-docs/stable/merging.html#joining-on-index

like image 127
Wes McKinney Avatar answered Oct 11 '22 22:10

Wes McKinney