Reputation: 399
I have dataframe like this:
0 1 2 3
{'id':10:'name':'Paul'} {'id':10:'age':33} {'id':10:'name':'Paul'} {'id':10:'name':'Paul'}
{'id':10:'name':'Paul'} {'id':10:'age':'Paul'} {'id':10:'name':'Paul'} {'id':10:'name':'Paul'}
{'id':10:'name':'Paul'} {'id':10:'name':'Paul'} {'id':10:'name':'Paul'} {'id':10:'name':'Paul'}
{'id':10:'name':'Paul'} {'id':10:'name':'Paul'} {'id':10:'name':'Paul'} None
So, in this example I have 4 columns with 4 rows. Happens that my dataframe has 500 rows and 7 columns, something around 3500 records (there are a few none
on column 7)
All rows in all columns are dict type, even though they can be different or not
What I need: How can I transform this dataframe to have only 1 column, by placing all row of column 1 below that last row of column 0 an so on untill all rows of the last column has been placed below the last row of column 0 ?
Expected Result:
0
{'id':10:'name':'Paul'}
{'id':10:'name':'Paul'}
{'id':10:'name':'Paul'}
{'id':10:'name':'Paul'}
{'id':10:'age':33}
{'id':10:'name':'Paul'}
{'id':10:'name':'Paul'}
{'id':10:'name':'Paul'}
{'id':10:'name':'Paul'}
{'id':10:'name':'Paul'}
more rows
None
Upvotes: 1
Views: 95
Reputation: 765
ss11=pd.Series()
def function1(ss:pd.Series):
global ss11
ss11=pd.concat([ss11,ss])
df1.apply(function1)
ss11.pipe(print)
0 {'id':10:'name':'Paul'}
1 {'id':10:'name':'Paul'}
2 {'id':10:'name':'Paul'}
3 {'id':10:'name':'Paul'}
0 {'id':10:'age':33}
1 {'id':10:'age':'Paul'}
2 {'id':10:'name':'Paul'}
3 {'id':10:'name':'Paul'}
0 {'id':10:'name':'Paul'}
1 {'id':10:'name':'Paul'}
2 {'id':10:'name':'Paul'}
3 {'id':10:'name':'Paul'}
0 {'id':10:'name':'Paul'}
1 {'id':10:'name':'Paul'}
2 {'id':10:'name':'Paul'}
3 None
Upvotes: 0
Reputation: 261280
You can melt
, and keep only the value column:
df.melt(value_name=0)[[0]]
output:
0
0 {'id':10:'name':'Paul'}
1 {'id':10:'name':'Paul'}
2 {'id':10:'name':'Paul'}
3 {'id':10:'name':'Paul'}
4 {'id':10:'age':33}
5 {'id':10:'age':'Paul'}
6 {'id':10:'name':'Paul'}
7 {'id':10:'name':'Paul'}
8 {'id':10:'name':'Paul'}
9 {'id':10:'name':'Paul'}
10 {'id':10:'name':'Paul'}
11 {'id':10:'name':'Paul'}
12 {'id':10:'name':'Paul'}
13 {'id':10:'name':'Paul'}
14 {'id':10:'name':'Paul'}
15 None
Upvotes: 1