python - Split pandas data frame columns by row value
问题描述
I have a data frame like this:
>df = pd.DataFrame({'A':['M',2,3],'B':['M',2,3],'AA':['N',20,30],'BB':['N',20,30]})
>df = df.rename(columns={df.columns[2]: 'A'})
>df = df.rename(columns={df.columns[3]: 'B'})
>df
A B A B
0 M M N N
1 2 2 20 20
2 3 3 30 30
and I have to split the data frame vertically by row index 0 = 'M' and 'N':
A B
0 M M
1 2 2
2 3 3
A B
0 N N
1 20 20
2 30 30
The data in the data frame comes from an Excel sheet and the column names are not unique. Thanks for help!
解决方案
这应该可以完成工作:
df.loc[:,df.iloc[0, :] == "M"]
df.loc[:,df.iloc[0, :] == "N"]
推荐阅读
- postgresql - 您将如何在一个 select 语句中将两个 jsonb 对象与 id 结合起来?
- android - 将密钥传递给下一个活动
- javascript - 如何使 select2 与所有 unicode 关键字一起工作?
- java - 如何使用 OptaPlanner 和 Spring Boot 减少 SoftScore?
- git - 使用 git 将本地项目上传到现有分支
- c# - 尝试通过在c#中的函数中传递单个值来返回多个值
- curl - 如何将 curl GET 请求转换为 Zapier Webhook 请求?
- mysql - 日期名称函数
- android - 深度链接参数不会通过 Mozilla Firefox 和 Opera 传递到 Android 应用程序
- c# - 如何更改 datetime.Now 没有特殊字符?