Text Pattern into Data Frame
Question:
import pandas as pd
text = '''
12/2/2023
Deposit
USD
10
Complete
15/2/2023
Deposit
USD
15
Complete
'''
x = text.split()
y=pd.DataFrame(x)
print(y)
I want convert this into data frame with colums ‘date’,’txn_type’,’curr’,’Amt’,’status’, any suggestions
Answers:
You can reshape the split text
x = np.array(text.split()).reshape(-1, 5)
y = pd.DataFrame(x, columns=['date','txn_type','curr','Amt','status'])
print(y)
date txn_type curr Amt status
0 12/2/2023 Deposit USD 10 Complete
1 15/2/2023 Deposit USD 15 Complete
import pandas as pd
text = '''
12/2/2023
Deposit
USD
10
Complete
15/2/2023
Deposit
USD
15
Complete
'''
x = text.split()
y=pd.DataFrame(x)
print(y)
I want convert this into data frame with colums ‘date’,’txn_type’,’curr’,’Amt’,’status’, any suggestions
You can reshape the split text
x = np.array(text.split()).reshape(-1, 5)
y = pd.DataFrame(x, columns=['date','txn_type','curr','Amt','status'])
print(y)
date txn_type curr Amt status
0 12/2/2023 Deposit USD 10 Complete
1 15/2/2023 Deposit USD 15 Complete