'DaraFrame write information to a DataFrame in a for loop
I would like to write text to a DataFrame in a For loop. Currently I only get the last line, however I would like to add line by line as the if statements become true. Here is my current Code:
sentence_only = ""
feature = ""
sentiment = ""
index = 0
is_first_row = True
for row in rawlist.split("\n"):
if is_first_row == False:
df=pd.DataFrame({"sentence_only": sentence_only, "feature": feature, "sentiment": sentiment}, index = [index])
if re.match(r"^##", row):
sentence_only = row[2:]
index+=1
if is_first_row == True:
df=pd.DataFrame({"sentence_only": sentence_only, "feature": feature, "sentiment": sentiment}, index = [index])
is_first_row = False
continue
df
My input:
## After that , it worked like a champ .
## No problems whatsoever .
incompatibility[-1] ## My only grips are the incompatibility with XP and the USB hub in the display .
## This is a well know problem with the PCs and Apple does not seem to care .
## Also , the only hard button controls you get are brightness and contrast .
Current output:
sentence_only feature sentiment
4 This is a well know problem with the PCs and Apple does not seem to care .
Desired output:
sentence_only feature sentiment
0 After that , it worked like a champ .
1 No problems whatsoever .
2 My only grips are the incompatibility with XP and the USB hub in the display . incompatibility
3 This is a well know problem with the PCs and Apple does not seem to care .
4 Also , the only hard button controls you get are brightness and contrast .
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|