简体   繁体   English

CSV 文件 - 使用 python 处理行和列

[英]CSV file - handle row and columns with python

Could you help me, please?请问你能帮帮我吗?

I have CSV dataset file about hotels and contain many columns.我有关于酒店的 CSV 数据集文件并包含许多列。 I need to handle hotel-name and reviews.我需要处理酒店名称和评论。

How to convert hotel-name rows to columns ?如何将酒店名称行转换为列? to combine reviews for each hotel and save the output into the new CSV file?合并每家酒店的评论并将输出保存到新的 CSV 文件中?

I use Python 3.7我使用 Python 3.7

Update: first thank you for comments更新:首先感谢您的评论

and I'm sorry, I have to supposed put the shape of the output对不起,我必须把输出的形状

I have more than 1400 hotels我有1400多家酒店

hotel-name     reviews 
Hotel Arena    love it
Hotel Arena    great
Hotel Arena    good
Hotel Arena    ........

the output will be :

hotel 1   hotel 2  hotel 3  .......
love it   stay     not bad
great     old      ..... 
good      ...      .......
..        
...         
....       

If I understand your question correctly, you have a csv file that contains many rows, each row has a review of a hotel.如果我正确理解您的问题,您有一个包含多行的 csv 文件,每一行都有对酒店的评论。

If so you can use something like this:如果是这样,你可以使用这样的东西:

import pandas as pd

df = pd.DataFrame({'hotel': ['A', 'A', 'B', 'B', 'A', 'C'], 'rating': [1, 1, 2, 4, 3, 5]})

df.groupby('hotel').aggregate(lambda x: list(x))

The DF output is: DF 输出为:

  hotel  rating
0     A       1
1     A       1
2     B       2
3     B       4
4     A       3
5     C       5

And after the group by:在分组之后:

          rating
hotel           
A      [1, 1, 3]
B         [2, 4]
C            [5]

You will have to install pandas for this, and read your csv file with pandas (which is very easy).您必须为此安装熊猫,并使用熊猫读取您的 csv 文件(这很容易)。

You can use CSV library to do that.您可以使用CSV 库来做到这一点。 Assuming a csv file like this one:假设一个像这样的 csv 文件:

name,review,comments
A,nice,blabla
B,notnice,bleble

You can filter the rows by columns:您可以按列过滤行:

import csv

if __name__ == "__main__":
    file = open('file.csv', 'rb')
    for row in csv.DictReader(file, delimiter = ','):
        print (row['name'],row['review']) 

Printing:印刷:

('A', 'nice')
('B', 'notnice')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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