[英]Python convert one column data into multiple columns
The question i have is: I have a dataset that looks like this (let's say there is one variable called "ChatConversations" and another called CustomerID) and that has the text of the chat for each customer.我的问题是:我有一个看起来像这样的数据集(假设有一个名为“ChatConversations”的变量和另一个名为 CustomerID 的变量),其中包含每个客户的聊天文本。 Suppose, there are 1000 customers, so my dataset has 1000 rows with 2 columns, one for CustomerID and another for the Chattranscript.
假设有 1000 个客户,所以我的数据集有 1000 行和 2 列,一列用于 CustomerID,另一列用于 Chattranscript。 Suppose each customer has 2 sentences each.
假设每个客户都有 2 个句子。 So, i want to create a new dataset/file, which has 2000 sentences appended to each other, like a paragraph,which i will then read and do text mining on.
所以,我想创建一个新的数据集/文件,其中有 2000 个相互附加的句子,就像一个段落,然后我将阅读并进行文本挖掘。
Hopefully my question is clear希望我的问题很清楚
Output data like below: I love thes service.IT took time.The issue was resolved, so I don't have complaints.The agent couldn't understand what I said.Grett job no complaints.Can do better (basically all the values of the "ChatCOnversation" variable need to be joined together to create a paragraph/text file kind of thing输出数据如下:我喜欢这些服务。IT 花了时间。问题解决了,所以我没有抱怨。代理无法理解我说的话。格雷特工作没有抱怨。可以做得更好(基本上所有值“ChatCONversation”变量需要连接在一起以创建段落/文本文件之类的东西
You could make a dictionary for each row which you create by zipping a list of column headlines and each data line together and store those in a list, like this:您可以通过将列标题列表和每个数据行压缩在一起并将它们存储在列表中来为您创建的每一行制作一个字典,如下所示:
headlines = ["India", "Asia", "Singapore", "Malaysia", "Nepal", "China"]
dict_list = []
with open("my_file.csv") as csv_file:
for line in csv_file:
dict_list.append(dict(zip(hl, [item.strip() for item in line.split(",")])))
print(*dict_list, sep="\n") # print one dictionary per line
You probably want just something like this:你可能只想要这样的东西:
with open("input.csv") as in_file, open("output.txt", "w") as out_file:
for line in in_file:
content = line.split(",", 1)[1].strip()
print(content, file=out_file)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.