简体   繁体   中英

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. Suppose, there are 1000 customers, so my dataset has 1000 rows with 2 columns, one for CustomerID and another for the Chattranscript. Suppose each customer has 2 sentences each. 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.

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

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

Update:

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)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM