new_comment数据不对,回到之前的代码def comment_p(x): x = x.replace(r'+','') if '万' in x: x = x.replace(r'万','') x=float(x)*10000 return x else: return xdf['new_comment'] = df['comment'].apply(lambda x:comment_p(x)).astype('int')
时间: 2024-03-22 21:40:30 浏览: 25
根据你提供的代码,`comment_p` 函数是用来处理 `comment` 列中的数据的。你可以尝试输出 `df['comment']` 和 `df['new_comment']` 的前几行来检查数据是否被正确处理。可以使用以下代码来输出:
```
print(df['comment'].head())
print(df['new_comment'].head())
```
如果输出的结果不对,可以检查一下 `comment_p` 函数的逻辑是否正确,或者检查一下输出的数据是否符合预期。
相关问题
代码2# 处理comment列数据 def comment_p(x): x = x.replace(r'+','') if '万' in x: x = x.replace(r'万','') x=float(x)*10000 return x else: return int(x)
根据您提供的代码,`comment_p` 函数会将带有 `+` 和 `万` 字符的字符串转换为整型或浮点数类型,并将结果返回。如果字符串中包含 `+` 字符,则将其替换为空字符串;如果包含 `万` 字符,则将其替换为空字符串并将结果乘以 `10000`。
您可以通过以下代码查看处理后的 `comment` 列的前5行数据:
```
# 处理comment列数据
def comment_p(x):
x = x.replace(r'+','')
if '万' in x:
x = x.replace(r'万','')
x = float(x) * 10000
return x
else:
return int(x)
df['new_comment'] = df['comment'].apply(lambda x: comment_p(x)).astype('int')
# 查看处理后的comment列数据的前5行
print(df['new_comment'].head())
```
这将显示处理后的 `comment` 列的前5行数据。
# 处理comment列数据 def comment_p(x): x = x.replace(r'+','') if '万' in x: x = x.replace(r'万','') x=float(x)*10000 return x else: return xdf['new_comment'] = df['comment'].apply(lambda x:comment_p(x)).astype('int')
根据您提供的代码,`comment_p` 函数中的 `return` 语句返回的是一个浮点数类型,但是 `df['new_comment']` 的数据类型被指定为整型,所以会报错。您可以将 `return` 语句中的浮点数强制转换为整型,如下所示:
```
# 处理comment列数据
def comment_p(x):
x = x.replace(r'+','')
if '万' in x:
x = x.replace(r'万','')
x = int(float(x) * 10000)
return x
else:
return int(x)
df['new_comment'] = df['comment'].apply(lambda x: comment_p(x)).astype('int')
```
这样可以确保 `df['new_comment']` 列中的数据类型是整型,避免了类型错误的出现。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)