【问题标题】:Delete strings from file using python使用python从文件中删除字符串
【发布时间】:2016-08-20 02:53:27
【问题描述】:

我有csv 文件

ID,"address","used_at","active_seconds","pageviews"
0a1d796327284ebb443f71d85cb37db9,"vk.com",2016-01-29 22:10:52,3804,115
0a1d796327284ebb443f71d85cb37db9,"2gis.ru",2016-01-29 22:48:52,214,24
0a1d796327284ebb443f71d85cb37db9,"yandex.ru",2016-01-29 22:14:30,4,2
0a1d796327284ebb443f71d85cb37db9,"worldoftanks.ru",2016-01-29 22:10:30,41,2

我需要删除包含一些单词的字符串。一共117个字。

我试试

for line in df:
    if 'yandex.ru' in line:
        df = df.replace(line, '')

但是对于 117 个单词,它的运行速度太慢了,在它之后我创建了 pivot_table,并且我尝试删除的单词包含在列中。

             aaa                         10ruslake.ru  youtube.ru 1tv.ru  24open.ru
0   0025977ab2998580d4559af34cc66a4e             0        0       34      43
1   00c651e018cbcc8fe7aa57492445c7a2             230      0       0       23
2   0120bc30e78ba5582617a9f3d6dfd8ca             12       0       0       0
3   01249e90ed8160ddae82d2190449b773             25       0       13      25

该列仅包含 0

我怎样才能更快地做到这一点并删除行以使单词不在列中?

【问题讨论】:

  • 抱歉,您正在遍历您的 df 列,然后测试是否存在一个单词并用空字符串替换?你的话在列表中吗?如果是这样你可以试试pattern = '|'.join(words)' for col in df: df[col] = df.str.replace(pattern, '', case=False)

标签: python excel csv pandas


【解决方案1】:

IIUC 你可以使用isinboolean indexing

print df
                                 ID          address              used_at  \
0  0a1d796327284ebb443f71d85cb37db9           vk.com  2016-01-29 22:10:52   
1  0a1d796327284ebb443f71d85cb37db9           vk.com  2016-01-29 22:10:52   
2  0a1d796327284ebb443f71d85cb37db9          2gis.ru  2016-01-29 22:48:52   
3  0a1d796327284ebb443f71d85cb37db9        yandex.ru  2016-01-29 22:14:30   
4  0a1d796327284ebb443f71d85cb37db9  worldoftanks.ru  2016-01-29 22:10:30   

   active_seconds  pageviews  
0            3804        115  
1            3804        115  
2             214         24  
3               4          2  
4              41          2  

words = ['vk.com','yandex.ru']

print ~df.address.isin(words)
0    False
1    False
2     True
3    False
4     True
Name: address, dtype: bool

print df[~df.address.isin(words)]
                                 ID          address              used_at  \
2  0a1d796327284ebb443f71d85cb37db9          2gis.ru  2016-01-29 22:48:52   
4  0a1d796327284ebb443f71d85cb37db9  worldoftanks.ru  2016-01-29 22:10:30   

   active_seconds  pageviews  
2             214         24  
4              41          2  

然后使用pivot:

print df[~df.address.isin(words)].pivot(index='ID', columns='address', values='pageviews')
address                           2gis.ru  worldoftanks.ru
ID                                                        
0a1d796327284ebb443f71d85cb37db9       24                2

另一种解决方案是删除行,当在某些列中是 0(例如 pageviews ):

print df

                                 ID          address              used_at  \
0  0a1d796327284ebb443f71d85cb37db9       youtube.ru  2016-01-29 22:10:52   
1            0a1d796327284ebfsffsdf       youtube.ru  2016-01-29 22:10:52   
2  0a1d796327284ebb443f71d85cb37db9           vk.com  2016-01-29 22:10:52   
3  0a1d796327284ebb443f71d85cb37db9          2gis.ru  2016-01-29 22:48:52   
4  0a1d796327284ebb443f71d85cb37db9        yandex.ru  2016-01-29 22:14:30   
5  0a1d796327284ebb443f71d85cb37db9  worldoftanks.ru  2016-01-29 22:10:30   

   active_seconds  pageviews  
0            3804          0  
1            3804          0  
2            3804        115  
3             214         24  
4               4          2  
5              41          2  
print df.pageviews != 0
0    False
1    False
2     True
3     True
4     True
5     True
Name: pageviews, dtype: bool

print df[(df.pageviews != 0)]
                                 ID          address              used_at  \
2  0a1d796327284ebb443f71d85cb37db9           vk.com  2016-01-29 22:10:52   
3  0a1d796327284ebb443f71d85cb37db9          2gis.ru  2016-01-29 22:48:52   
4  0a1d796327284ebb443f71d85cb37db9        yandex.ru  2016-01-29 22:14:30   
5  0a1d796327284ebb443f71d85cb37db9  worldoftanks.ru  2016-01-29 22:10:30   

   active_seconds  pageviews  
2            3804        115  
3             214         24  
4               4          2  
5              41          2  

print df[(df.pageviews != 0)].pivot_table(index='ID', columns='address', values='pageviews')
address                           2gis.ru  vk.com  worldoftanks.ru  yandex.ru
ID                                                                           
0a1d796327284ebb443f71d85cb37db9       24     115                2          2

【讨论】:

  • 我向另一个帐户stackoverflow.com/questions/36839602/… 提问。我需要删除带有一些url 的列。我尝试删除原始csv 文件中的字符串,然后创建一个pivot_table
  • 嗯,您需要删除数据中的一些行,其中address 列包含一些字符串?还是子串?在枢轴之前?如果是,您需要isinboolean indexing
  • 次要问题df[df.pageviews != 0] 不是更易读吗?
【解决方案2】:

我知道处理 csv 文件的最快方法是使用包 Pandas 从中创建数据框。

import pandas as pd

df = pd.read_csv(the_path_of_your_file,header = 0)
df.ix[df.ix[:,'address'] == 'yandex.ru','address'] = ''

这会将包含“yandex.ru”的单元格替换为具有空字符串的单元格。 然后您可以将其写回为 csv:

df.to_csv(the_path_of_your_file)

如果您想要删除该 url 出现的行,请使用:

df = df.drop(df[df.address == 'yandex.ru'].index)

【讨论】:

    猜你喜欢
    • 2021-09-24
    • 2022-07-22
    • 1970-01-01
    • 1970-01-01
    • 2020-03-27
    • 1970-01-01
    • 1970-01-01
    • 2018-10-18
    • 2017-04-01
    相关资源
    最近更新 更多