根据我的理解,我认为PCA只能用于连续功能.但是,虽然试图理解onehot编码和标签编码之间的区别来自以下链接中的帖子:
何时使用One Hot Encoding vs LabelEncoder vs DictVectorizor?
它指出PCA之后的一个热编码是一种非常好的方法,这基本上意味着PCA应用于分类特征.因此困惑,请建议我一样.
有没有办法将Spark Df(不是RDD)转换为熊猫DF
我尝试了以下方法:
var some_df = Seq(
("A", "no"),
("B", "yes"),
("B", "yes"),
("B", "no")
).toDF(
"user_id", "phone_number")
Run Code Online (Sandbox Code Playgroud)
码:
%pyspark
pandas_df = some_df.toPandas()
Run Code Online (Sandbox Code Playgroud)
错误:
NameError: name 'some_df' is not defined
Run Code Online (Sandbox Code Playgroud)
有什么建议么。
code: df['review'].head()
index review
output: 0 These flannel wipes are OK, but in my opinion
Run Code Online (Sandbox Code Playgroud)
我想从数据框的列中删除标点符号并创建一个新列.
code: import string
def remove_punctuations(text):
return text.translate(None,string.punctuation)
df["new_column"] = df['review'].apply(remove_punctuations)
Error:
return text.translate(None,string.punctuation)
AttributeError: 'float' object has no attribute 'translate'
Run Code Online (Sandbox Code Playgroud)
我正在使用python 2.7.任何的意见都将会有帮助.
For a given list of tuples, if multiple tuples in the list have the first element of tuple the same - among them select only the tuple with the maximum last element.
For example:
sample_list = [(5,16,2),(5,10,3),(5,8,1),(21,24,1)]
Run Code Online (Sandbox Code Playgroud)
在sample_list上面,因为5在这种情况下前 3 个元组具有相似的第一个元素,其中只有第二个元组应该保留,因为它具有最大的最后一个元素 => 3。
预期操作:
op = [(5,10,3),(21,24,1)]
Run Code Online (Sandbox Code Playgroud)
代码:
op = []
for m in range(len(sample_list)):
li = [sample_list[m]]
for n in range(len(sample_list)):
if(sample_list[m][0] == sample_list[n][0]
and sample_list[m][2] != sample_list[n][2]):
li.append(sample_list[n])
op.append(sorted(li,key=lambda dd:dd[2],reverse=True)[0])
print (list(set(op)))
Run Code Online (Sandbox Code Playgroud)
这有效。但是对于长列表来说非常慢。有没有更pythonic或更有效的方法来做到这一点?
我想从另一个jupyter笔记本导入一个函数
在n1.ipynb中:
def test_func(x):
return x + 1
-> run this
Run Code Online (Sandbox Code Playgroud)
在n2.ipynb中:
%%capture
%%run n1.ipynb
test_func(2)
Run Code Online (Sandbox Code Playgroud)
错误:
NameError Traceback (most recent call last)<ipython-input-2-4255cde9aae3> in <module>()
----> 1 test_func(1)
NameError: name 'test_func' is not defined
Run Code Online (Sandbox Code Playgroud)
有什么简单的方法吗?
我有一个示例 DF,试图用升序排序索引替换列值列表:
DF:
df = pd.DataFrame(np.random.randint(0,10,size=(7,3)),columns=["a","b","c"])
df["d1"]=["Apple","Mango","Apple","Mango","Mango","Mango","Apple"]
df["d2"]=["Orange","lemon","lemon","Orange","lemon","Orange","lemon"]
df["date"] = ["2002-01-01","2002-01-01","2002-01-01","2002-01-01","2002-02-01","2002-02-01","2002-02-01"]
df["date"] = pd.to_datetime(df["date"])
a b c d1 d2 date
0 2 7 9 Apple Orange 2002-01-01
1 6 0 9 Mango lemon 2002-01-01
2 8 0 0 Apple lemon 2002-01-01
3 4 4 4 Mango Orange 2002-01-01
4 5 0 8 Mango lemon 2002-02-01
5 6 1 6 Mango Orange 2002-02-01
6 7 2 7 Apple lemon 2002-02-01
Run Code Online (Sandbox Code Playgroud)
第1步:
Group the DF by "date" column, sample group on "2002-01-01" …Run Code Online (Sandbox Code Playgroud) 这是我的熊猫数据帧:
Item Support_Count
0 BREAD 4
1 MILK 4
2 DIAPER 4
3 BEER 3
Run Code Online (Sandbox Code Playgroud)
如何从第1列"项目"中生成2和3组项目的所有可能的唯一组合.
示例(2项目集):(面包,牛奶),(面包,尿布),(面包,啤酒),(牛奶,尿布)等.
实施例(3个集):(面包,牛奶,尿布),(面包,牛奶,啤酒),(牛奶,尿布,啤酒)等
df1:
Timestamp:
1995-08-01T00:00:01.000+0000
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以使用pyspark在数据框的时间戳列中分隔月份中的某天。无法提供代码,我是新手。我不知道如何进行。
df:
cat116_O cat116_S cat116_T cat116_U cat116_Y
0 0.0 0.0 0.0 0.0 0.0
1 0.0 0.0 0.0 0.0 0.0
expected output:
df(changed):
cat116_O cat116_S cat116_T cat116_U cat116_Y
0 -1 -1 -1 -1 -1
1 -1 -1 -1 -1 -1
code:
df.replace(0.0, -1)
Run Code Online (Sandbox Code Playgroud)
但它不起作用。我能够对每一行和每一列进行迭代,但这需要很多时间。代码中的替换功能哪里出了问题。
我正在使用Zeppelin笔记本在Scala中创建Spark脚本。
码:
def test: DataFrame= {
//code
}
Run Code Online (Sandbox Code Playgroud)
我收到以下警告:
warning: there was one deprecation warning; re-run with -deprecation for details.
Run Code Online (Sandbox Code Playgroud)
关于如何使用“ -deprecation”运行功能的任何建议?
PS。我不是在询问警告,因为这将需要功能的完整代码。我正在寻找有关如何在齐柏林飞艇笔记本中使用折旧参数重新运行该功能的建议。该问题的现有解决方案仅适用于SBT。
python ×7
pandas ×5
apache-spark ×3
python-3.x ×2
data-mining ×1
dataframe ×1
pyspark ×1
replace ×1
scala ×1
scikit-learn ×1
string ×1