Is it possible to add several columns at once to a pandas DataFrame?

dbliss

If I want to create a new DataFrame with several columns, I can add all the columns at once -- for example, as follows:

data = {'col_1': [0, 1, 2, 3],
        'col_2': [4, 5, 6, 7]}
df = pd.DataFrame(data)

But now suppose farther down the road I want to add a set of additional columns to this DataFrame. Is there a way to add them all simultaneously, as in

additional_data = {'col_3': [8, 9, 10, 11],
                   'col_4': [12, 13, 14, 15]}
#Below is a made-up function of the kind I desire.
df.add_data(additional_data)

I'm aware I could do this:

for key, value in additional_data.iteritems():
    df[key] = value

Or this:

df2 = pd.DataFrame(additional_data, index=df.index)
df = pd.merge(df, df2, on=df.index)

I was just hoping for something cleaner. If I'm stuck with these two options, which is preferred?

Zero

Pandas has assign method since 0.16.0. You could use it on dataframes like

In [1506]: df1.assign(**df2)
Out[1506]:
   col_1  col_2  col_3  col_4
0      0      4      8     12
1      1      5      9     13
2      2      6     10     14
3      3      7     11     15

or, you could directly use the dictionary like

In [1507]: df1.assign(**additional_data)
Out[1507]:
   col_1  col_2  col_3  col_4
0      0      4      8     12
1      1      5      9     13
2      2      6     10     14
3      3      7     11     15

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pandas: adding several columns to a dataframe in a single line

From Dev

Fill na on several columns of a Pandas Dataframe

From Dev

How to edit/add two columns to a dataframe in pandas at once - df.apply()

From Dev

How do I add several columns at once in kdb?

From Dev

Possible to create sparse Pandas DataFrame with string columns?

From Dev

Pandas DataFrame GroupBy by several columns with mean, StdDev and count statistics

From Dev

Pandas multiply several multi-index columns in a dataframe by another column

From Dev

How do I split a string into several columns in a dataframe with pandas Python?

From Dev

Apply function on dataframe Column to get several other columns Pandas Python

From Dev

Pandas DataFrame GroupBy by several columns with mean, StdDev and count statistics

From Dev

pandas dataframe: groupby by several columns, apply function and map back the result

From Dev

Adding column in pandas with several conditions based on other columns in dataframe

From Dev

Rank several columns in a dataframe

From Dev

make several operations in a dataframe at once

From Dev

How to get value counts for multiple columns at once in Pandas DataFrame?

From Dev

Changing certain values in multiple columns of a pandas DataFrame at once

From Java

Add multiple empty columns to pandas DataFrame

From Dev

how to add columns label on a Pandas DataFrame

From Dev

Add multiple columns to a Pandas dataframe quickly

From Dev

Add Multiple Columns to Pandas Dataframe from Function

From Dev

Add values and columns in specific order to Pandas DataFrame

From Dev

How to add columns to an empty pandas dataframe?

From Dev

Pandas DataFrame.add() -- ignore missing columns

From Dev

Pandas: Add new column with several values to groupby dataframe

From Dev

Is it possible to import several packages at once in Java?

From Dev

Numpy: vectorized access of several columns at once?

From Dev

Edit the levels of several columns at once in R

From Dev

Searching across several columns of a dataframe

From Dev

All possible permutations columns Pandas Dataframe within the same column

Related Related

  1. 1

    Pandas: adding several columns to a dataframe in a single line

  2. 2

    Fill na on several columns of a Pandas Dataframe

  3. 3

    How to edit/add two columns to a dataframe in pandas at once - df.apply()

  4. 4

    How do I add several columns at once in kdb?

  5. 5

    Possible to create sparse Pandas DataFrame with string columns?

  6. 6

    Pandas DataFrame GroupBy by several columns with mean, StdDev and count statistics

  7. 7

    Pandas multiply several multi-index columns in a dataframe by another column

  8. 8

    How do I split a string into several columns in a dataframe with pandas Python?

  9. 9

    Apply function on dataframe Column to get several other columns Pandas Python

  10. 10

    Pandas DataFrame GroupBy by several columns with mean, StdDev and count statistics

  11. 11

    pandas dataframe: groupby by several columns, apply function and map back the result

  12. 12

    Adding column in pandas with several conditions based on other columns in dataframe

  13. 13

    Rank several columns in a dataframe

  14. 14

    make several operations in a dataframe at once

  15. 15

    How to get value counts for multiple columns at once in Pandas DataFrame?

  16. 16

    Changing certain values in multiple columns of a pandas DataFrame at once

  17. 17

    Add multiple empty columns to pandas DataFrame

  18. 18

    how to add columns label on a Pandas DataFrame

  19. 19

    Add multiple columns to a Pandas dataframe quickly

  20. 20

    Add Multiple Columns to Pandas Dataframe from Function

  21. 21

    Add values and columns in specific order to Pandas DataFrame

  22. 22

    How to add columns to an empty pandas dataframe?

  23. 23

    Pandas DataFrame.add() -- ignore missing columns

  24. 24

    Pandas: Add new column with several values to groupby dataframe

  25. 25

    Is it possible to import several packages at once in Java?

  26. 26

    Numpy: vectorized access of several columns at once?

  27. 27

    Edit the levels of several columns at once in R

  28. 28

    Searching across several columns of a dataframe

  29. 29

    All possible permutations columns Pandas Dataframe within the same column

HotTag

Archive