我想转换一个列,其中元素的类型与字符混合的类型,我想将列转换为Integer类型.
Input:
df = pd.DataFrame({'id':['Q001','Q021']})
Run Code Online (Sandbox Code Playgroud)
Output:
id
0 Q001
1 Q021
Run Code Online (Sandbox Code Playgroud)
Expected:
id idInt
0 Q001 1
1 Q021 21
Run Code Online (Sandbox Code Playgroud) 例如,我有一个名为的DataFrame a.我想要计算每一行的元素.
import numpy as np
a=pd.DataFrame({'A1':['financial','game','game'],'A2':['social','food','sport'],'A3':['social','sport','game']})
Run Code Online (Sandbox Code Playgroud)
Input:
A1 A2 A3
0 financial social social
1 game food sport
2 game sport game
Run Code Online (Sandbox Code Playgroud)
Expected:
financial food game social sport
0 1 0 0 2 0
1 0 1 1 0 1
2 0 0 2 0 1
Run Code Online (Sandbox Code Playgroud)
希望能得到帮助,谢谢!
例如,我有一个df与nan和使用下面的方法来fillna.
import pandas as pd
a = [[2.0, 10, 4.2], ['b', 70, 0.03], ['x', ]]
df = pd.DataFrame(a)
print(df)
df.fillna(int(0),inplace=True)
print('fillna df\n',df)
dtype_df = df.dtypes.reset_index()
Run Code Online (Sandbox Code Playgroud)
OUTPUT:
0 1 2
0 2 10.0 4.20
1 b 70.0 0.03
2 x NaN NaN
fillna df
0 1 2
0 2 10.0 4.20
1 b 70.0 0.03
2 x 0.0 0.00
col type
0 0 object
1 1 float64
2 2 float64
Run Code Online (Sandbox Code Playgroud)
实际上,我希望column 1维持类型int …
例如,我有一个带有两列的df.
输入
df = pd.DataFrame({'user_id':list('aaabbbccc'),'label':[0,0,1,0,0,2,0,1,2]})
print('df\n',df)
Run Code Online (Sandbox Code Playgroud)
产量
df
label user_id
0 0 a
1 0 a
2 1 a
3 0 b
4 0 b
5 2 b
6 0 c
7 1 c
8 2 c
Run Code Online (Sandbox Code Playgroud)
我想label分别按user_id 计算group中的元素.预期输出如下所示.
预期
df
label user_id label_0 label_1 label_2
0 0 a 2 1 0
1 0 a 2 1 0
2 1 a 2 1 0
3 0 b 2 0 1
4 0 b 2 0 1
5 2 …Run Code Online (Sandbox Code Playgroud) 我想检查列是否app包含元素myList.
import pandas as pd
df=pd.DataFrame({'app':['a,b,c','e,f']})
myList=['b', 'f']
print(df)
Run Code Online (Sandbox Code Playgroud)
Output:
app
0 a,b,c
1 e,f
Run Code Online (Sandbox Code Playgroud)
Expected:
app contains_b contains_f
0 a,b,c 1 0
1 e,f 0 1
Run Code Online (Sandbox Code Playgroud) 我想通过等效分割将数据帧列的连续值转换为离散值。例如,以下是我的input。
我想将列中的连续值a分成3个间隔。
Input:
import pandas as pd
import numpy as np
df = pd.DataFrame({'a':[1.1, 1.2, 1.3, 2.4, 2.5, 4.1]})
Run Code Online (Sandbox Code Playgroud)
Output:
a
0 1.1
1 1.2
2 1.3
3 2.4
4 2.5
5 4.1
Run Code Online (Sandbox Code Playgroud)
在列中 a,最小值为1.1,最大值为4.1,我想将其划分为3 intervals。
如您所见,每个间隔的大小等于(4.1-1.1)/3 = 1.0。因此,我可以将[1.1, 2.1)(大于或等于1.1和小于2.1)0间隔中的所有值都[2.1, 3.1)视为1,as 间隔中的所有值以及[3.1, 4.1]as 间隔中的所有值2。
所以这是我的预期结果。
Expected:
a
0 0
1 0
2 0 …Run Code Online (Sandbox Code Playgroud) 在这里,我想写一个函数来排序a。
我想这样排序a。
a = [[1,3,2],[1,2,3],[2,3,2],[2,3,1]]
def sort(a, sort_index):
if len(set([_[sort_index] for _ in a])) < len(a):
key_list = [sort_index] + [i for i in range(len(a[0]))]
# sort by multi keys.
a = sorted(a, key=lambda x: (x[i] for i in range(key_list)))
return a
sort(a,0)
Run Code Online (Sandbox Code Playgroud)
这里,sort_index是第一个重要指标。如果 中的值相同sort_index,则将考虑其他索引。所以,结果是预期的:
a = [[1,2,3],[1,3,2],[2,3,1],[2,3,2]]
Run Code Online (Sandbox Code Playgroud)