小编stu*_*ent的帖子

在熊猫中删除中文

我正在尝试从包含拉丁文和中文字符的 csv 中删除所有中文字符。数据看起来像:

    address                                                 lat
1   ?????, Zhangjiang, Pudong New District, 203718       31.204024
2   ??, 3057?, Jinke Road, Pudong, 201203, China          31.181804
Run Code Online (Sandbox Code Playgroud)

我需要它看起来像:

    address                                                 lat
1   , Zhangjiang, Pudong New District, 203718               31.204024
2   , 3057, Jinke Road, Pudong, 201203, China               31.181804
Run Code Online (Sandbox Code Playgroud)

我尝试过df.replace(/[^\x00-\x7F]/g, "")df.replace(/[\u{0080}-\u{FFFF}]/gu,"")但出现错误:

    df1.replace([^\x00-\x7F],"");
                 ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

需要帮忙!谢谢

python string replace dataframe pandas

5
推荐指数
1
解决办法
5023
查看次数

LabelEncoder.fit_transform()的类型错误

我正在使用Kaggle的Titanic数据集(https://www.kaggle.com/c/titanic/data),我想使用sklearn.preprocessing中的LabelEncoder转换Sex,最初标记为'male'或'female '进'0'或'1'.我有以下四行代码,

import pandas as pd
from sklearn.preprocessing import LabelEncoder
df = pd.read_csv('titanic.csv')
df['Sex'] = LabelEncoder.fit_transform(df['Sex'])  
Run Code Online (Sandbox Code Playgroud)

但是当我运行它时,我收到以下错误消息:

TypeError: fit_transform() missing 1 required positional argument: 'y'
Run Code Online (Sandbox Code Playgroud)

错误来自第4行,即

df['Sex'] = LabelEncoder.fit_transform(df['Sex']) 
Run Code Online (Sandbox Code Playgroud)

我想知道这里出了什么问题.虽然我知道我也可以使用map进行转换,这可能更简单,但我仍然想知道我对LabelEncoder的使用有什么问题.

python-3.x pandas scikit-learn

4
推荐指数
1
解决办法
3051
查看次数

从过去 2 年的 Python 中随机选择一个月份

我想在当前年份和 2016 年之间随机选择一个月份。这是我目前非常幼稚的解决方案

from random import choice
def get_month():
    return choice({'2018-06','2018-05','2018-04','2018-03'})
Run Code Online (Sandbox Code Playgroud)

很明显,这个集合在未来会变得太大,那么实现这一目标的更好方法是什么?

python random

4
推荐指数
1
解决办法
1260
查看次数

如何解决TypeError:'float'对象不可调用

import math

def reportSphereVolume(r):
    SphereVolume = ((4/3)*math.pi*((r)**3))
    return SphereVolume


def reportSphereSurfaceArea(r):
    SphereSurfaceArea = ((4)*math.pi((r)**2))
    return SphereSurfaceArea

radius = int(input("What is the radius of the sphere? " ))
reportSphereVolume(radius)
reportSphereSurfaceArea(radius)
Run Code Online (Sandbox Code Playgroud)

执行时,我收到以下内容.

What is the radius of the sphere? 10
Traceback (most recent call last):
  File "D:\Thonny\SphereAreaVolume.py", line 16, in <module>
    reportSphereSurfaceArea(radius)
  File "D:\Thonny\SphereAreaVolume.py", line 11, in reportSphereSurfaceArea
    SphereSurfaceArea = ((4)*math.pi((r)**2))
TypeError: 'float' object is not callable
Run Code Online (Sandbox Code Playgroud)

我迷路了,我一直在看视频和阅读教科书,但我仍然无法解决.请帮忙.

python python-3.x

4
推荐指数
1
解决办法
2934
查看次数

标准缩放器和MinMaxScaler之间的区别

MinMaxScaler和标准缩放器之间有什么区别.

MMS= MinMaxScaler(feature_range = (0, 1)) (用于Program1)

sc = StandardScaler() (在另一个程序中,他们使用标准缩放器而不是minMaxScaler)

python data-science

3
推荐指数
3
解决办法
4368
查看次数

python中的范围函数

for n in range(2,5):
    for x in range(2,n):
        print(n,x)
Run Code Online (Sandbox Code Playgroud)

输出为:

3 2
4 2
4 3
Run Code Online (Sandbox Code Playgroud)

为什么n的值从3开始而不是2?

python range

0
推荐指数
1
解决办法
198
查看次数