Reputation: 445
I have a dataframe that only contains one row, but multiple columns:
I want to put every 5 columns to a new row. Here's the expected output:
The original data was in a list, I converted to a dataframe. I don't know if it's easier to reshape through a list, but here's a sample list for you to try out, the original list is really long. ['review: I stayed around 11 days and enjoyed stay very much.', 'compound: 0.5106, ','neg: 0.0, ','neu: 0.708, ','pos: 0.292, ','review: Plans for weekend stay canceled due to Coronavirus shutdown.','compound: 0.0, ','neg: 0.0, ','neu: 1.0, ','pos: 0.0, ']
Upvotes: 0
Views: 215
Reputation: 4130
You can do this easily using numpy
import numpy as np
import pandas as pd
lis = np.array(['review: I stayed around 11 days and enjoyed stay very much.', 'compound: 0.5106, ','neg: 0.0, ','neu: 0.708, ','pos: 0.292, ','review: Plans for weekend stay canceled due to Coronavirus shutdown.','compound: 0.0, ','neg: 0.0, ','neu: 1.0, ','pos: 0.0, '])
columns = 5
t = np.char.split(lis,":")
cols,vals = list(zip(*t))
dff = pd.DataFrame(np.split(np.array(vals),len(vals)/columns),
columns=cols[:columns]).replace(",","",regex=True)
Upvotes: 0
Reputation: 71
You can try using dictionary
lst = ['review: I stayed around 11 days and enjoyed stay very much.', 'compound: 0.5106, ','neg: 0.0, ','neu: 0.708, ','pos: 0.292, ',
'review: Plans for weekend stay canceled due to Coronavirus shutdown.','compound: 0.0, ','neg: 0.0, ','neu: 1.0, ','pos: 0.0, ']
from collections import defaultdict
import pandas as pd
data_dict = defaultdict(list)
for _ in lst:
header, value = _.split(':')
data_dict [header].append(value.strip())
pd.DataFrame.from_dict(data_dict)
Upvotes: 0
Reputation: 11
def main():
data_new = ['review: I stayed around 11 days and enjoyed stay very much.', 'compound: 0.5106, ','neg: 0.0, ','neu: 0.708, ','pos: 0.292, ','review: Plans for weekend stay canceled due to Coronavirus shutdown.','compound: 0.0, ','neg: 0.0, ','neu: 1.0, ','pos: 0.0, ']
len_data = len(data_new)
proc_row_mul_of_five = len_data / 5
j = 5
k = 0
for i in range(0,proc_row_mul_of_five):
print(data_new[k:j])
k = i + 5
j = j + 5
main()
Upvotes: 0
Reputation: 11342
It's easier to parse it as a list, then convert it to a dataframe.
Try this:
import pandas as pd
lst = ['review: I stayed around 11 days and enjoyed stay very much.', 'compound: 0.5106, ','neg: 0.0, ','neu: 0.708, ','pos: 0.292, ',
'review: Plans for weekend stay canceled due to Coronavirus shutdown.','compound: 0.0, ','neg: 0.0, ','neu: 1.0, ','pos: 0.0, ']
dd = {}
for x in lst:
sp = x.split(':')
if sp[0] in dd:
dd[sp[0]].append(sp[1].replace(',',"").strip())
else:
dd[sp[0]] = [sp[1].replace(',',"").strip()]
print(dd)
print(pd.DataFrame(dd).to_string(index=False))
Output
review compound neg neu pos
I stayed around 11 days and enjoyed stay very much. 0.5106 0.0 0.708 0.292
Plans for weekend stay canceled due to Coronavirus shutdown. 0.0 0.0 1.0 0.0
Upvotes: 1