Add new column to dataframe using .assign()?

Screen Link:

My Code:

df = pd.DataFrame(bmp_series, columns=['mean_price'])
df = df.assign(bmm_series, columns=['mean_odometer_km'])

What I expected to happen:
Expecting bmm (i.e. brand_mean_mileage) to be appended as a column to df.

What actually happened:

TypeErrorTraceback (most recent call last)
<ipython-input-19-268e96f16d16> in <module>()
      1 df = pd.DataFrame(bmp_series, columns=['mean_price'])
----> 2 df = df.assign(bmm_series, columns=['mean_odometer_km'])

TypeError: assign() takes 1 positional argument but 2 were given

hey @jp.wall

the syntax is different for .assign method. You may review the official doc for same:

I tried this one example:

you could use this as well to add new column

brands_df.insert(1, 'mean_mileage', bmm_series)


I use the example code, it works!

  • convert top6_mean_price dictionay to Series, then convert it toaggregate_data DataFrame:
    bmp_series = pd.Series(top6_mean_price)
    aggregate_data = pd.DataFrame(bmp_series, columns=[‘mean_price’])

  • convert top6_mean_mileage dictionay to Series, then assign it as a new column in aggregate_data DataFrame:
    bmm_series = pd.Series(top6_mean_mileage)
    aggregate_data[‘mean_milage’] = bmm_series

-For the first dictionary, you can conver it to DataFrame directly:
df =pd.DataFrame.from_dict(top6_mean_price, orient=‘index’)
df.columns = [“mean_price”]