[英]convert dataframe column into textfile (iterating rows to line)
我在熊貓數據框中有這樣的東西:
Name,Total,Problem
Toyota,2,'They don’t need the oil consumed per trip, only a sum of manually counted damage.'
Mazda,1,'Hello, I got an engine error. See attachment below.'
Kia,2,'Client requires a confirmation that the information provided through CODEXXXX asap.'
Volkswagon,3,'During visual inspection of lights we have found a damage.'
我對獲取df['Problem']
列並將其轉換為文本文件感興趣,因此文本文件的輸出如下:
They don’t need the oil consumed per trip, only a sum of manually counted damage.
Hello, I got an engine error. See attachment below.
Client requires a confirmation that the information provided through CODEXXXX asap.
During visual inspection of lights we have found a damage.
我什至不需要文本文件中的索引,因為它將在文本標記應用程序中使用(並且必須以該格式輸入)。 即使df['Problem']
的行/元素是一段長字符串,它也必須位於文本行的一行中。
試試下面的代碼:
with open('sample.txt','w') as f:
for data in list(df['Problem']):
f.write(data+'\n')
我希望這將有所幫助。
Series的tolist()
方法可以在此處完成大部分工作。 您可以使用join
在內存中構建整個文本(如果df不太大):
with open(file, 'w') as out:
print('\n'.join(df['Problem'].tolist()), file=out)
如果要節省內存,也可以逐個元素打印:
with open(file, 'w') as out:
for line in df['Problem'].tolist():
print(line, file=out)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.