[英]change column values each specific column
我正在围绕着将近200列和70000行的大型数据集进行游戏。 这是一个混乱的数据,所以我应该使它更具可读性。
数据列中的平均值是: ATT_A(agree)
, ATT_SA(Strongly agree)
, ATT_D(disagree)
等等
每5列仅代表1个答案
我的想法是,我可以使用.replace()
函数,然后使每1个值列表示一个值(如果列名.._ SA,则列值应为'SA'
而不是1)
然后我可以将5列合并为一列。 它将减少混乱。
IDEA_COLUMN
SA
A
SD
A
D
SA
在这里,我尝试了我的代码。
for c in cols.columns:
if c.upper()[:4] == 'ATT_':
if c[-2:] == 'SA':
c.replace('1', 'SA')
我尝试了很多不同类型的很多次,但是我看不到自己的错误。 我是编码新手,所以我会犯一些愚蠢的错误。
这是一个选择:
# split the columns at the second underscore to make the columns a multi-index
df.columns = df.columns.str.rsplit("_", n=1, expand=True)
# transform the answer A,SA,D... to a column, group by level 0(row number) and find out the
# answer corresponding to 1 with idxmax
df.stack(level=1).groupby(level=0).agg(lambda x: x.idxmax()[1])
另一种选择 :
# split columns as above
df.columns = df.columns.str.rsplit("_", n=1, expand=True)
# group columns based on the prefix along axis 1, and for each row find out the index with
# value 1 using idxmax() function
df.groupby(level=0, axis=1).apply(lambda g: g.apply(lambda x: x.idxmax()[1], axis = 1))
数据设置 :
cols1 = ["ATT_TECHIMP_" + x for x in ["SA", "A", "NO", "D", "SD"]]
cols2 = ["ATT_BBB_" + x for x in ["SA", "A", "NO", "D", "SD"]]
df1 = pd.DataFrame([[1, None, None, None, None], [None, None, 1, None, None], [None, None, 1, None, None], [None, None, None, 1, None], [None, None, None, None, 1]], columns=cols1)
df2 = pd.DataFrame([[None, 1, None, None, None], [None, None, None, None, 1], [None, None, 1, None, None], [None, None, None, 1, None], [None, None, None, None, 1]], columns=cols2)
df = pd.concat([df1, df2], axis=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.