【问题标题】:Generate new field csv python生成新字段 csv python
【发布时间】:2018-09-26 01:20:01
【问题描述】:

我有这个 csv 文件:

movieId;title;genres
1;Toy Story (1995);Adventure|Animation|Children|Comedy|Fantasy
2;Jumanji (1995);Adventure|Children|Fantasy
3;Grumpier Old Men (1995);Comedy|Romance
4;Waiting to Exhale (1995);Comedy|Drama|Romance
5;Father of the Bride Part II (1995);Comedy
6;Heat (1995);Action|Crime|Thriller
7;Sabrina (1995);Comedy|Romance
8;Tom and Huck (1995);Adventure|Children
9;Hate (Haine, La) (1995);Crime|Drama
10;Seven (a.k.a. Se7en) (1995);Mystery|Thriller

我想从字段标题生成一个名为 year 的新字段,因为字段标题还包含电影的年份。 我用这种方法试过了,但是不行:

import pandas
df=pandas.read_csv("/Users/Desktop/IMDB.csv")
str=df
str1="(19"
str2="(20"
str3="(21"
str.find(str1, beg=0, end=len(string))
str.find(str1, beg=0, end=len(string)) 
str.find(str1, beg=0, end=len(string))

【问题讨论】:

    标签: python pandas csv dataset


    【解决方案1】:

    如果包含长度为 4 的数字,则通过正则表达式将 str.extract 用于括号之间的值:

    df['year'] = df['title'].str.extract('\((\d{4})\)', expand=False).astype(int)
    print (df)
       movieId                               title  \
    0        1                    Toy Story (1995)   
    1        2                      Jumanji (1995)   
    2        3             Grumpier Old Men (1995)   
    3        4            Waiting to Exhale (1995)   
    4        5  Father of the Bride Part II (1995)   
    5        6                         Heat (1995)   
    6        7                      Sabrina (1995)   
    7        8                 Tom and Huck (1995)   
    8        9             Hate (Haine, La) (1995)   
    9       10         Seven (a.k.a. Se7en) (1995)   
    
                                            genres  year  
    0  Adventure|Animation|Children|Comedy|Fantasy  1995  
    1                   Adventure|Children|Fantasy  1995  
    2                               Comedy|Romance  1995  
    3                         Comedy|Drama|Romance  1995  
    4                                       Comedy  1995  
    5                        Action|Crime|Thriller  1995  
    6                               Comedy|Romance  1995  
    7                           Adventure|Children  1995  
    8                                  Crime|Drama  1995  
    9                             Mystery|Thriller  1995  
    

    【讨论】:

    • 我写了这个:import pandasdf=pandas.read_csv("/Users/Desktop/IMDB.csv")df['year'] = df['title'].str.extract('\((\d{4})\)', expand=False).astype(int)print (df)
    • @P.ru - 是吗?还有什么问题?
    • 它直到一分钟前才起作用,现在如果我启动 .py 我得到这个:Traceback (most recent call last): File "script.py", line 3, in <module> df['year'] = df['title'].str.extract('\((\d{4})\)', expand=False).astype(int) File "/Library/Python/2.7/site-packages/pandas/core/frame.py", line 2139, in __getitem__ return self._getitem_column(key) File "/Library/Python/2.7/site-packages/pandas/core/frame.py", line 2146, in _getitem_column... File "pandas/_libs/hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 'title'
    • @P.ru - 表示没有列title,返回print (df.columns.tolist())是什么?
    • 返回同样的错误。但是我发布的代码直到几分钟前才起作用,我不知道为什么它不再起作用了。
    猜你喜欢
    • 2023-04-05
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-26
    • 2019-05-15
    • 1970-01-01
    • 2011-09-26
    相关资源
    最近更新 更多