Splitting multiple columns into rows in pandas dataframe

You can first split columns, create Series by stack and remove whitespaces by strip: s1 = df.value.str.split(‘,’, expand=True).stack().str.strip().reset_index(level=1, drop=True) s2 = df.date.str.split(‘,’, expand=True).stack().str.strip().reset_index(level=1, drop=True) Then concat both Series to df1: df1 = pd.concat([s1,s2], axis=1, keys=[‘value’,’date’]) Remove old columns value and date and join: print (df.drop([‘value’,’date’], axis=1).join(df1).reset_index(drop=True)) ticker account value date 0 aa assets 100 20121231 … Read more

(pandas) Create new column based on first element in groupby object

You need transform with first: print (df.groupby(‘Person’)[‘Color’].transform(‘first’)) 0 blue 1 green 2 orange 3 blue 4 green 5 orange Name: Color, dtype: object df[‘First_Col’] = df.groupby(‘Person’)[‘Color’].transform(‘first’) print (df) Color Person First_Col 0 blue bob blue 1 green jim green 2 orange joe orange 3 yellow bob blue 4 pink jim green 5 purple joe orange

using time zone in pandas to_datetime

You can use tz_localize to set the timezone to UTC/+0000, and then tz_convert to add the timezone you want: start = pd.to_datetime(‘2015-02-24’) rng = pd.date_range(start, periods=10) df = pd.DataFrame({‘Date’: rng, ‘a’: range(10)}) df.Date = df.Date.dt.tz_localize(‘UTC’).dt.tz_convert(‘Asia/Kolkata’) print (df) Date a 0 2015-02-24 05:30:00+05:30 0 1 2015-02-25 05:30:00+05:30 1 2 2015-02-26 05:30:00+05:30 2 3 2015-02-27 05:30:00+05:30 3 … Read more

Jupyter notebook display two pandas tables side by side

I have ended up writing a function that can do this: [update: added titles based on suggestions (thnx @Antony_Hatchkins et al.)] from IPython.display import display_html from itertools import chain,cycle def display_side_by_side(*args,titles=cycle([”])): html_str=”” for df,title in zip(args, chain(titles,cycle([‘</br>’])) ): html_str+='<th style=”text-align:center”><td style=”vertical-align:top”>’ html_str+=f'<h2 style=”text-align: center;”>{title}</h2>’ html_str+=df.to_html().replace(‘table’,’table style=”display:inline”‘) html_str+='</td></th>’ display_html(html_str,raw=True) Example usage: df1 = pd.DataFrame(np.arange(12).reshape((3,4)),columns=[‘A’,’B’,’C’,’D’,]) df2 = … Read more

Trouble installing Pandas on new MacBook Air M1

Maybe it is too late. But the only solution worked for me is installing from source if you do not want to use rosetta2 or moniconda python3 -m pip install virtualenv virtualenv -p python3.8 venv source venv/bin/activate pip install –upgrade pip pip install numpy cython git clone –depth 1 https://github.com/pandas-dev/pandas.git cd pandas python3 setup.py install

tech