python中apply(lambda x:x.split(':')[0]).tolist() 作用
时间: 2023-12-02 12:25:46 浏览: 198
这行代码是将一个列表中的每个元素都通过 `split(':')` 方法以 `:` 为分隔符进行分割,然后只取每个元素中的第一个部分,即分隔符前面的部分,最后将所有的结果转换为列表并返回。
具体来说,这里使用了 lambda 函数作为参数传递给 `apply()` 方法,其中 `x` 表示列表中的每个元素。在 lambda 函数中,`split(':')` 方法将每个元素以 `:` 为分隔符进行分割,并返回一个列表,然后 `[0]` 取该列表的第一个元素,即分隔符前面的部分。最后使用 `tolist()` 方法将所有的结果转换为列表并返回。
相关问题
data = dataset.copy() # 折扣率处理 is_manjian = data['Discount_rate'].str.contains(':').astype(int) discount_rate = data['Discount_rate'].apply(lambda x: float(x) if ':' not in str(x) else (float(str(x).split(':')[0]) - float(str(x).split(':')[1])) / float(str(x).split(':')[0])) min_cost_of_manjian = data['Discount_rate'].apply(lambda x: -1 if ':' not in str(x) else int(str(x).split(':')[0])) data['is_manjian'] = is_manjian data['discount_rate'] = discount_rate data['min_cost_of_manjian'] = min_cost_of_manjian # 距离处理 data['Distance'].fillna(-1, inplace=True) null_distance = (data['Distance'] == -1).astype(int) data['null_distance'] = null_distance # 时间处理 data['date_received'] = pd.to_datetime(data['Date_received'], format='%Y%m%d') if 'Date' in data.columns.tolist(): data['date'] = pd.to_datetime(data['Date'], format='%Y%m%d') return data出现cannot convert float NaN to integer的报错怎么解决
出现 `cannot convert float NaN to integer` 的报错是因为在 `null_distance` 的计算中,`data['Distance']` 中存在缺失值 `NaN`,无法将其转换为整数类型。可以尝试将 `null_distance` 的计算改为:
```python
null_distance = (data['Distance'].fillna(-1) == -1).astype(int)
```
这样可以先将 `NaN` 值填充为 -1,再进行计算,避免了无法转换类型的问题。
优化以下代码,# 构建特征矩阵和标签向量 X = [] y = data['Rating'] for index, row in data.iterrows(): features = [] # 添加运行时长区间评分 if pd.notna(row['RunTime']): category1 = pd.cut([row['RunTime']], bins=bins1, labels=labels1)[0] if category1 in avg_runtime_ratings: features.append(avg_runtime_ratings[category1]) else: features.append(0) else: features.append(0) # 添加年份区间评分 if pd.notna(row['year']): category2 = pd.cut([row['year']], bins=bins2, labels=labels2)[0] if category2 in avg_year_ratings: features.append(avg_year_ratings[category2]) else: features.append(0) else: features.append(0) # 添加导演评分 if row.Director in avg_director_ratings: features.append(avg_director_ratings[row.Director]) else: features.append(0) # 添加编剧评分 if row.Writer in avg_writer_ratings: features.append(avg_writer_ratings[row.Writer]) else: features.append(0) # 添加主演评分 casts = row.TopTwoCasts.split(',') if len(casts) == 1: cast = casts[0] if cast in avg_casts_ratings: features.append(avg_casts_ratings[cast]) else: features.append(0) features.extend([0, 0]) else: cast_1, cast_2 = casts if cast_1 in avg_casts_ratings: features.append(avg_casts_ratings[cast_1] * 0.6) else: features.append(0) if cast_2 in avg_casts_ratings: features.append(avg_casts_ratings[cast_2] * 0.4) else: features.append(0) # 添加类型评分 genres = row.Genres.split(',') if len(genres) == 1: genre = genres[0] if genre in avg_genres_ratings: features.append(avg_genres_ratings[genre]) else: features.append(0) features.extend([0, 0]) elif len(genres) == 2: genre_1, genre_2 = genres if genre_1 in avg_genres_ratings: features.append(avg_genres_ratings[genre_1] * 0.6) else: features.append(0) if genre_2 in avg_genres_ratings: features.append(avg_genres_ratings[genre_2] * 0.4) else: features.append(0) features.append(0) else: genre_1, genre_2, genre_3 = genres if genre_1 in avg_genres_ratings: features.append(avg_genres_ratings[genre_1] * 0.4) else: features.append(0) if genre_2 in avg_genres_ratings: features.append(avg_genres_ratings[genre_2] * 0.3) else: features.append(0) if genre_3 in avg_genres_ratings: features.append(avg_genres_ratings[genre_3] * 0.3) else: features.append(0) X.append(features) X = pd.DataFrame(X)
可以优化的地方有:
1. 使用向量化操作代替迭代操作,可以显著提高代码效率;
2. 缩短代码长度和提高可读性,可以通过使用函数和列表推导式等技巧实现。
以下是优化后的代码:
```python
import numpy as np
def get_feature(row, avg_runtime_ratings, avg_year_ratings, avg_director_ratings, avg_writer_ratings, avg_casts_ratings, avg_genres_ratings):
features = []
# 添加运行时长区间评分
runtime = row.get('RunTime', np.nan)
category1 = pd.cut([runtime], bins=bins1, labels=labels1)[0] if pd.notna(runtime) else None
if category1 in avg_runtime_ratings:
features.append(avg_runtime_ratings[category1])
else:
features.append(0)
# 添加年份区间评分
year = row.get('year', np.nan)
category2 = pd.cut([year], bins=bins2, labels=labels2)[0] if pd.notna(year) else None
if category2 in avg_year_ratings:
features.append(avg_year_ratings[category2])
else:
features.append(0)
# 添加导演评分
director = row.get('Director', None)
features.append(avg_director_ratings.get(director, 0))
# 添加编剧评分
writer = row.get('Writer', None)
features.append(avg_writer_ratings.get(writer, 0))
# 添加主演评分
casts = row.get('TopTwoCasts', '').split(',')
if len(casts) == 1:
cast = casts[0]
features.append(avg_casts_ratings.get(cast, 0))
features.extend([0, 0])
else:
cast_1, cast_2 = casts
features.append(avg_casts_ratings.get(cast_1, 0) * 0.6)
features.append(avg_casts_ratings.get(cast_2, 0) * 0.4)
# 添加类型评分
genres = row.get('Genres', '').split(',')
if len(genres) == 1:
genre = genres[0]
features.append(avg_genres_ratings.get(genre, 0))
features.extend([0, 0])
elif len(genres) == 2:
genre_1, genre_2 = genres
features.append(avg_genres_ratings.get(genre_1, 0) * 0.6)
features.append(avg_genres_ratings.get(genre_2, 0) * 0.4)
features.append(0)
else:
genre_1, genre_2, genre_3 = genres
features.append(avg_genres_ratings.get(genre_1, 0) * 0.4)
features.append(avg_genres_ratings.get(genre_2, 0) * 0.3)
features.append(avg_genres_ratings.get(genre_3, 0) * 0.3)
return features
X = data.apply(lambda row: get_feature(row, avg_runtime_ratings, avg_year_ratings, avg_director_ratings, avg_writer_ratings, avg_casts_ratings, avg_genres_ratings), axis=1)
X = pd.DataFrame(X.tolist())
```
阅读全文