Reputation: 844
So I have a list of series as the following:
groupdf1
Out[304]:
[90-95 9
>100 1
80-90 1
50-80 1
Name: bucket, dtype: int64, 80-90 9
95-100 1
90-95 1
50-80 1
Name: bucket, dtype: int64, 80-90 10
90-95 1
50-80 1
Name: bucket, dtype: int64, 80-90 11
95-100 1
Name: bucket, dtype: int64, 50-80 9
What I'm trying is to create a dataframe like the following:
bucket.1 bucket.2 bucket.3 bucket.4
90-95 9 1 1 NaN
>100 1 NaN NaN NaN
80-90 1 9 10 11
50-80 1 1 1 NaN
95-100 NaN NaN NaN 1
which is basically merging each of the series on the index. I'm not being able to run it in a loop. I'm getting the following error:
groupdf=pd.DataFrame(groupdf1[0])
for i in range(1,len(groupdf1)):
groupdf2=pd.DataFrame(groupdf1[i])
groupdf.concat(groupdf2)
Traceback (most recent call last):
File "<ipython-input-278-4c8876ecdb74>", line 4, in <module>
groupdf.concat(groupdf2)
File "C:\Users\hp1\Anaconda3\lib\site-packages\pandas\core\generic.py", line 3081, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'concat'
Can somebody please help me out here? Thanks.
Upvotes: 1
Views: 2323
Reputation: 862481
If groupdf1
is list of DataFrame
s use pandas.concat
with axis=1
and then set new columns names by enumerate
with f-string
s:
df = pd.concat(groupdf1, axis=1)
df.columns = [f'{x}.{i}' for i, x in enumerate(df.columns, 1)]
#alternative
#df.columns = [f'bucket.{i}' for i in np.arange(1,len(df.columns) + 1)]
Upvotes: 4
Reputation: 12515
It’s pandas.concat ... a DataFrame object doesn’t have that attribute. Try pandas.concat((df1, df2)), or whatever your dataframes are named. As jezrael pointed out, pd.concat takes any interval of dataframes, so just pass the axis=1 parameter to concatenate, column-wise.
Upvotes: 1