If I have a dataframe with only two datatypes like below:
d = {'col1': [1, 2], 'col2': ['jack', 'bill'], 'col3': [4, 5], 'col4': ['megan', 'sarah']}
df = pd.DataFrame(data=d)
print(df)
col1 col2 col3 col4
0 1 jack 4 megan
1 2 bill 5 sarah
print(df.dtypes)
col1 int64
col2 object
col3 int64
col4 object
Is there a way to stack these columns based only on data type? The end result would be:
col1 col2
0 1 jack
1 2 bill
2 4 megan
3 5 sarah
It's not necessary for the final column names to remain the same.