I have a data set as such of 2 order array, with arbitrary length. as shown below
[['15,39' '17,43']
['23,40' '18,44']
['28,41' '18,45']
['28,42' '27,46']
['34,43' '26,47']
.
.
.
]
I want to turn it into a panda dataframe as columns and rows, shown below
15 39 17 43
23 40 18 44
28 41 18 45
28 42 27 46
34 43 26 47
.
.
.
anyone has idea how to achieve it without saving the data out to files during process?
Importing pandas would be the first step
import pandas as pd
Now, we have to save the 2d array into the list
As the output dataframe contains only integer datatype we have to remove all the quotes and add comas where needed. For example
lst = [[ 15, 39, 17, 43],[ 23, 40, 18, 44],[ 28, 41, 18, 45],[ 28, 42, 27, 46],[ 34, 43, 26, 47 ]]
now using the pandas library we convert the list into a dataframe
df = pd.DataFrame(lst,columns = [ 'cl1 ', 'cl2', 'cl3', 'cl4' ] )
Now the only thing remaining is to print and check the output dataframe.
print(df)
You can convert the list you have to a pandas dataframe like this,
import pandas as pd
data = \
[['15,39', '17,43'],
['23,40', '18,44'],
['28,41', '18,45'],
['28,42', '27,46'],
['34,43', '26,47']]
df = pd.DataFrame(data)
Now, to add columns like you want,
df.pop(0).str.split(",", expand=True).rename(columns={0: "col_0", 1: "col_1"}) \
.join(df.pop(1).str.split(",", expand=True).rename(columns={0: "col_2", 1: "col_3"}))
col_0 col_1 col_2 col_3
0 15 39 17 43
1 23 40 18 44
2 28 41 18 45
3 28 42 27 46
4 34 43 26 47
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.