BFTM BFTM - 3 days ago 6
Python Question

pandas: combine two columns in a DataFrame

I have a pandas DataFrame that has multiple columns in it:

Index: 239897 entries, 2012-05-11 15:20:00 to 2012-06-02 23:44:51
Data columns:
foo 11516 non-null values
bar 228381 non-null values
Time_UTC 239897 non-null values
dtstamp 239897 non-null values
dtypes: float64(4), object(1)


where
foo
and
bar
are columns which contain the same data yet are named differently. Is there are a way to move the rows which make up
foo
into
bar
, ideally whilst maintaining the name of
bar
?

In the end the DataFrame should appear as:

Index: 239897 entries, 2012-05-11 15:20:00 to 2012-06-02 23:44:51
Data columns:
bar 239897 non-null values
Time_UTC 239897 non-null values
dtstamp 239897 non-null values
dtypes: float64(4), object(1)


That is the NaN values that made up bar were replaced by the values from
foo
.

Answer

Try this:

pandas.concat([df['foo'].dropna(), df['bar'].dropna()]).reindex_like(df)

If you want that data to become the new column bar, just assign the result to df['bar'].

Comments