'How to concat on axis 1 and fill all rows in pandas
This is my current code that works but is slow:
pivot_df = pd.pivot_table(df, values = ["val1","val2"], index=['date'], columns = 'category2')
df = pd.merge(df,pivot_df,on="date",how="left",suffixes=["","_pivot")
But in my dataframe, there is only 1 unique date. So I can simply do this to speed up merge:
pivot_df = pd.pivot_table(df, values = ["val1","val2"], index=['date'], columns = 'category2').reset_index(drop=True)
pivot_df.columns = [str(col)+"_pivot" for col in pivot_df.columns.tolist()]
df = pd.concat([df,company_pivot],axis=1)
But since pivot_df shape is 1x226 and df shape is 550x2700. The concat method just fill the first row of df.
Solution 1:[1]
I know this one dirty solution, but is there any better way?
pivot_df = pd.pivot_table(df, values = ["val1","val2"], index=['date'], columns = 'category2').reset_index(drop=True)
pivot_df.columns = [str(col)+"_pivot" for col in pivot_df.columns.tolist()]
pivot_df = pd.concat([pivot_df]*10,axis=0,ignore_index=True)
pivot_df = pd.concat([pivot_df]*55,axis=0,ignore_index=True)
df = pd.concat([df,company_pivot],axis=1)
Just creating duplicate rows in 2 lines, since 1 line seems slower.
Also, If I even add more rows dynamically, this becomes a more mess.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
