如何在pandas数据框中添加新列,并为所有值<= W1插入1,为所有值<= W2插入2,为所有值> W2插入3?
W1=3
W2=6
Run Code Online (Sandbox Code Playgroud)
这是我的示例:
column1 number
2 1
1 1
5 2
6 2
7 3
8 3
3 1
Run Code Online (Sandbox Code Playgroud) 我想保留一个具有每个id最大系列的记录.所以对于每个id,我需要一行.我想我需要类似的东西
df_new = df.groupby('id')['series'].nlargest(1)
Run Code Online (Sandbox Code Playgroud)
,但那肯定是错的.
这就是我的数据集的外观:
id series s1 s2 s3
1 2 4 9 1
1 8 6 2 2
1 3 9 1 3
2 9 4 1 5
2 2 2 5 5
2 5 1 7 8
3 6 7 2 3
3 2 4 4 1
3 1 3 9 9
Run Code Online (Sandbox Code Playgroud)
这应该是结果:
id series s1 s2 s3
1 8 6 2 2
2 9 4 1 5
3 6 7 2 3
Run Code Online (Sandbox Code Playgroud) 我想以降序将特征选择的结果可视化为条形图。(仅前 10 个特征)我如何使用 matplotlib 做到这一点?在下面你可以看到代码。
filename_train = 'C:\Users\x.x\workspace\Dataset\x.csv'
names = ['a', 'b', 'c', 'd', 'e' ...........]
df_train = pd.read_csv(filename_train, names=names)
array = df_train.values
X = array[:,0:68]
Y = df_train['RUL'].values
import numpy as np
from sklearn.feature_selection import SelectKBest
# feature extraction
test = SelectKBest(score_func=f_regression, k=10)
fit = test.fit(X, Y)
# summarize scores
np.set_printoptions(precision=2)
print(fit.scores_)
Run Code Online (Sandbox Code Playgroud) 我想标准化我的数据并计算皮尔逊相关性。如果我在没有标准化的情况下尝试这个,它就会起作用。通过规范化,我收到此错误消息: AttributeError: 'numpy.ndarray' object has no attribute 'corr' 我能做些什么来解决这个问题?
import numpy as np
import pandas as pd
filename_train = 'C:\Users\xxx.xxx\workspace\Dataset\!train_data.csv'
names = ['a', 'b', 'c', 'd', 'e', ...]
df_train = pd.read_csv(filename_train, names=names)
from sklearn.preprocessing import Normalizer
normalizeddf_train = Normalizer().fit_transform(df_train)
#pearson correlation
pd.set_option('display.width', 100)
pd.set_option('precision', 2)
print(normalizeddf_train.corr(method='pearson'))
Run Code Online (Sandbox Code Playgroud) 我有一个pandas数据框,想要添加一个新列.对于'number'中小于15的所有值,我想添加1,对于所有更大的值,0.我尝试了不同的方法,但我没有收到所需的结果.特别是,因为我有问题结构体.这是我想做的事情:
number binary
12 1
89 0
12 1
56 0
62 0
2 1
657 0
5 1
73 0
Run Code Online (Sandbox Code Playgroud) 我有两个数据框,ground_truth 和预测(都是熊猫系列)。最后,我想像我已经做的那样绘制所有预测点和所有ground_truth 点。我想要做的是在每个预测和 ground_truth 点之间绘制一条线。所以这条线是预测点x1,y1和ground_truth点x2,y2之间的连接。为了更好的理解,我附上了一张图片。黑线(通过油漆创建)是我想要做的。
这是我已经拥有的:
fig, ax = plt.subplots()
ax.plot(pred,'ro', label='Prediction', color = 'g')
ax.plot(GT,'^', label='Ground Truth', color = 'r' )
plt.xlabel('a')
plt.ylabel('b')
plt.title('test')
plt.xticks(np.arange(-1, 100, 5))
plt.style.use('ggplot')
plt.legend()
plt.show()
Run Code Online (Sandbox Code Playgroud) 我想绘制按ID分组的时间序列.所以那个时间是我的x值,'value'是我的y值.如何绘制由'id'1分组的x和y?
id time value
1 1 0.3
1 2 0.6
1 3 0.9
2 1 0.1
2 2 0.3
2 3 0.6
3 1 0.2
3 2 0.4
3 3 0.5
Run Code Online (Sandbox Code Playgroud) python ×7
numpy ×5
pandas ×5
matplotlib ×3
scikit-learn ×2
binary ×1
pearson ×1
prediction ×1
python-2.7 ×1