Gambit1614
Gambit1614

Reputation: 8801

Splitting rows into multiple columns in Pandas

So I have this dataframe :

df = pd.DataFrame(['Function_1','internal_prop_1','external_prop_1','Function_2','internal_prop_2','external_prop_2','Function_3','internal_prop_3','external_prop_3'], columns=['Raw_info'])


          Raw_info
0       Function_1
1  internal_prop_1
2  external_prop_1
3       Function_2
4  internal_prop_2
5  external_prop_2
6       Function_3
7  internal_prop_3
8  external_prop_3

I basically want to create a new dataframe with the following formate

       Function_name     prop1              prop2
0       Function_1    internal_prop_1    external_prop_1
1       Function_2    internal_prop_2    external_prop_2
2       Function_3    internal_prop_3    external_prop_3

In other words, I want to split every set of three rows into different columns. The closest I have got is this answer which does not solve my problem. Is there a Pythonic or efficient way to do this in Pandas, instead of iterating over every alternate 3 rows and doing it manually ?

Upvotes: 3

Views: 6204

Answers (1)

cs95
cs95

Reputation: 402493

Retrieve df.Raw_info.values, reshape the array and create a new dataframe with the pd.DataFrame constructor.

df = pd.DataFrame(df.Raw_info.values.reshape(-1, 3), 
                   columns=['Function_name', 'prop1', 'prop2'])

print(df) 
  Function_name            prop1            prop2
0    Function_1  internal_prop_1  external_prop_1
1    Function_2  internal_prop_2  external_prop_2
2    Function_3  internal_prop_3  external_prop_3

Upvotes: 3

Related Questions