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
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 .
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.
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.
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.
Do df.join(df2)
:
http://pandas.pydata.org/pandas-docs/stable/merging.html#joining-on-index
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With