我在这里提出另一个问题
我有一个大型数据框,大约有20列400.000行。在这个数据集中,我不能拥有字符串,因为处理数据的软件只接受数字和空值。
所以我认为它可能会起作用。 1.通过每一栏 2.获取唯一字符串列表 3.用0到X之间的值替换每个字符串 4.重复下一列的过程 5.对下一个数据帧重复
这是数据框的样子
DATE TIME FNRHP306H FNRHP306HC FNRHP306_2MEC_MAX
7-Feb-15 0:00:00 NORMAL NORMAL 1050
7-Feb-15 0:01:00 NORMAL NORMAL 1050
7-Feb-15 0:02:00 NORMAL HIGH 1050
7-Feb-15 0:03:00 HIGH NORMAL 1050
7-Feb-15 0:04:00 LOW NORMAL 1050
7-Feb-15 0:05:00 NORMAL LOW 1050
这是预期的结果
DATE TIME FNRHP306H FNRHP306HC FNRHP306_2MEC_MAX
7-Feb-15 0:00:00 0 0 1050
7-Feb-15 0:01:00 0 0 1050
7-Feb-15 0:02:00 0 1 1050
7-Feb-15 0:03:00 1 0 1050
7-Feb-15 0:04:00 2 0 1050
7-Feb-15 0:05:00 0 2 1050
我正在使用python 3.5和最新版本的Pandas
提前致谢
JV
答案 0 :(得分:1)
<强>解决方案:强>
# try to convert all columns to numbers...
df = df.apply(lambda x: pd.to_numeric(x, errors='ignore'))
cols = df.filter(like='FNR').select_dtypes(include=['object']).columns
st = df[cols].stack().to_frame('name')
st['cat'] = pd.factorize(st.name)[0]
df[cols] = st['cat'].unstack()
del st
<强>演示:强>
In [233]: df
Out[233]:
DATE TIME FNRHP306H FNRHP306HC FNRHP306_2MEC_MAX
0 7-Feb-15 0:00:00 NORMAL NORMAL 1050
1 7-Feb-15 0:01:00 NORMAL NORMAL 1050
2 7-Feb-15 0:02:00 NORMAL HIGH 1050
3 7-Feb-15 0:03:00 HIGH NORMAL 1050
4 7-Feb-15 0:04:00 LOW NORMAL 1050
5 7-Feb-15 0:05:00 NORMAL LOW 1050
首先我们stack所有object
(字符串)列:
In [235]: cols = df.filter(like='FNR').select_dtypes(include=['object']).columns
In [236]: st = df[cols].stack().to_frame('name')
现在我们可以factorize堆积列:
In [238]: st['cat'] = pd.factorize(st.name)[0]
In [239]: st
Out[239]:
name cat
0 FNRHP306H NORMAL 0
FNRHP306HC NORMAL 0
1 FNRHP306H NORMAL 0
FNRHP306HC NORMAL 0
2 FNRHP306H NORMAL 0
FNRHP306HC HIGH 1
3 FNRHP306H HIGH 1
FNRHP306HC NORMAL 0
4 FNRHP306H LOW 2
FNRHP306HC NORMAL 0
5 FNRHP306H NORMAL 0
FNRHP306HC LOW 2
将unstacked结果分配回原始DF(到object
列):
In [241]: df[cols] = st['cat'].unstack()
In [242]: df
Out[242]:
DATE TIME FNRHP306H FNRHP306HC FNRHP306_2MEC_MAX
0 7-Feb-15 0:00:00 0 0 1050
1 7-Feb-15 0:01:00 0 0 1050
2 7-Feb-15 0:02:00 0 1 1050
3 7-Feb-15 0:03:00 1 0 1050
4 7-Feb-15 0:04:00 2 0 1050
5 7-Feb-15 0:05:00 0 2 1050
<强>解释强>
In [248]: df.filter(like='FNR')
Out[248]:
FNRHP306H FNRHP306HC FNRHP306_2MEC_MAX
0 NORMAL NORMAL 1050
1 NORMAL NORMAL 1050
2 NORMAL HIGH 1050
3 HIGH NORMAL 1050
4 LOW NORMAL 1050
5 NORMAL LOW 1050
In [249]: df.filter(like='FNR').select_dtypes(include=['object'])
Out[249]:
FNRHP306H FNRHP306HC
0 NORMAL NORMAL
1 NORMAL NORMAL
2 NORMAL HIGH
3 HIGH NORMAL
4 LOW NORMAL
5 NORMAL LOW