我有一个数据帧X.我想将它转换为只有5个元素的1D数组.一种方法是将内部数组转换为列表.我怎样才能做到这一点?
0 1 2 3 4 5
0 1622 95 1717 85.278544 1138.964373 1053.685830
1 62 328 390 75.613900 722.588235 646.974336
2 102 708 810 75.613900 800.916667 725.302767
3 102 862 964 75.613900 725.870370 650.256471
4 129 1380 1509 75.613900 783.711111 708.097211
val = X.values将给出一个numpy数组.我想将数组的内部元素转换为列表.我怎样才能做到这一点?我试过这个却失败了
M = val.values.tolist()
A = np.array(M,dtype=list)
N = np.array(M,dtype=object)
Run Code Online (Sandbox Code Playgroud) 我有2个具有相同列标题的数据框。我希望对它们两个都进行热编码。我不能一一表演。我希望将两个数据帧附加在一起,然后执行热编码,然后将它们拆分为2个数据帧,并在每个数据帧上再次放置标头。
下面的代码一个接一个地执行热编码,而不是合并它们然后进行热编码。
train = pd.get_dummies(train, columns= ['is_discount', 'gender', 'city'])
test = pd.get_dummies(test, columns= ['is_discount', 'gender', 'city'])
Run Code Online (Sandbox Code Playgroud) 我尝试使用for循环实现2D数组的跨步卷积
arr = np.array([[2,3,7,4,6,2,9],
[6,6,9,8,7,4,3],
[3,4,8,3,8,9,7],
[7,8,3,6,6,3,4],
[4,2,1,8,3,4,6],
[3,2,4,1,9,8,3],
[0,1,3,9,2,1,4]])
arr2 = np.array([[3,4,4],
[1,0,2],
[-1,0,3]])
def stride_conv(arr1,arr2,s,p):
beg = 0
end = arr2.shape[0]
final = []
for i in range(0,arr1.shape[0]-1,s):
k = []
for j in range(0,arr1.shape[0]-1,s):
k.append(np.sum(arr1[beg+i : end+i, beg+j:end+j] * (arr2)))
final.append(k)
return np.array(final)
stride_conv(arr,arr2,2,0)
Run Code Online (Sandbox Code Playgroud)
这导致3*3阵列:
array([[ 91, 100, 88],
[ 69, 91, 117],
[ 44, 72, 74]])
Run Code Online (Sandbox Code Playgroud)
是否有一个numpy函数或scipy函数来做同样的事情?我的做法并不那么好.我该如何对此进行矢量化?
假设我有一个数据框和单词列表,即
toxic = ['bad','horrible','disguisting']
df = pd.DataFrame({'text':['You look horrible','You are good','you are bad and disguisting']})
main = pd.concat([df,pd.DataFrame(columns=toxic)]).fillna(0)
samp = main['text'].str.split().apply(lambda x : [i for i in toxic if i in x])
for i,j in enumerate(samp):
for k in j:
main.loc[i,k] = 1
Run Code Online (Sandbox Code Playgroud)
这将导致 :
bad disguisting horrible text
0 0 0 1 You look horrible
1 0 0 0 You are good
2 1 1 0 you are bad and disguisting
Run Code Online (Sandbox Code Playgroud)
这比 get_dummies 快一点,但是当有大量数据时,pandas 中的 for 循环并不明显。
我试过str.get_dummies …
我发现文档说如果可能的话减少返回类型的维度,否则返回一致的类型。
df = pd.DataFrame(
{'a': np.ones(4, dtype='float32'),
'b': np.ones(4, dtype='float32'),
'c': np.zeros(4, dtype='float32')})
df.groupby(df4.index,squeeze=True)['b'].sum()
Run Code Online (Sandbox Code Playgroud)
无论有没有挤压,我都看不到任何变化。有人可以向我解释一下挤压 = True 的真正目的以及为什么它默认设置为 false
假设我有一个数据框
micolumns = pd.MultiIndex.from_tuples([('a', 'foo'), ('a', 'bar'),
('b', 'foo'), ('b', 'bah')])
miindex=np.arange(3)
dfmi = pd.DataFrame(np.arange(3 * len(micolumns)).reshape((len(miindex), len(micolumns))),
index=miindex, columns=micolumns).sort_index().sort_index(axis=1)
a b
bar foo bah foo
0 1 0 3 2
1 5 4 7 6
2 9 8 11 10
Run Code Online (Sandbox Code Playgroud)
我想在零级的列中添加前缀,我该怎么做?
目前我正在重命名如下列,感觉多余。
df_new = dfmi.rename(columns=dict(zip(dfmi.columns.levels[0],'pre_'+dfmi.columns.levels[0])),level=0)
pre_a pre_b
bar foo bah foo
0 1 0 3 2
1 5 4 7 6
2 9 8 11 10
Run Code Online (Sandbox Code Playgroud)
add_prefix函数没有级别参数。有没有类似的功能?
从这个问题跟进
像这样旋转后我得到了一个数据框。
AVG GrossProfit AVG PMV Loss% Sales ParentAuction Copart IAA Copart IAA Copart IAA Copart IAA 制作 讴歌 112.99 NaN -15.53 NaN 36.46 NaN 96.0 NaN
如何将列级别更改为这种列格式?
ParentAuction Copart IAA
AVG GrossProfit AVG PMV 损失% 销售额 AVG GrossProfit AVG PMV 损失% 销售额
制作
讴歌 112.99 -15.53 36.46 96.0 NaN NaN NaN NaN
我有一个数据帧:
routeId latitude_value longitude_value
r1 28.210216 22.813209
r2 28.216103 22.496735
r3 28.161786 22.842318
r4 28.093110 22.807081
r5 28.220370 22.503500
r6 28.220370 22.503500
r7 28.220370 22.503500
Run Code Online (Sandbox Code Playgroud)
从这里我想生成一个像这样的数据帧df2:
routeId nearest
r1 r3 (for example)
r2 ... similarly for all the routes.
Run Code Online (Sandbox Code Playgroud)
我试图实现的逻辑是
对于每条路线,我应该找到所有其他路线的欧氏距离.并在routeId上迭代它.
有一个计算欧氏距离的功能.
dist = math.hypot(x2 - x1, y2 - y1)
Run Code Online (Sandbox Code Playgroud)
但我很困惑如何构建一个函数,我将传递一个数据帧,或使用.apply()
def get_nearest_route():
.....
return df2
Run Code Online (Sandbox Code Playgroud) 我有这样的数据框:
A B C D E
0 2 3 4 8 7
1 4 7 5 9 4
2 3 4 5 7 2
3 8 9 1 3 7
Run Code Online (Sandbox Code Playgroud)
我需要做这样的事情:
if 'value in column A' == 2:
'value for this row in new column' = 'value from column B' + 'value from column C'
elif 'value in column A' == 4:
'value for this row in new column' = 'value from column B' + 'value from column D'
elif …Run Code Online (Sandbox Code Playgroud) 我有一个带有 True 和 False 值的 DataFrame。
A B C D 0 假 真 真 假 1 假 假 真 假 2 真真假假
我想用列名填充真值,用 0 填充假值。我该怎么做?
即得到结果为
A B C D 0 0 公元前 0 1 0 0 C 0 2 AB 0 0
python ×10
pandas ×9
dataframe ×4
numpy ×3
arrays ×1
convolution ×1
levels ×1
mask ×1
multi-index ×1
pivot-table ×1
scipy ×1