python - 如何将数据框导出到现有的格式化 csv 文件?
问题描述
我正在尝试将数据框导出到现有的格式化 csv 文件,但数据框继续以垂直形式附加,以及应该水平的附加标题。
A B C D E F
1 2 3 4 5 6 #=-> This is the format I have in my exisiting csv file
A B C D E F
1 2 3 4 5 6
x x x x x x #-> This is how I want to do it
A B C D E F
1 2 3 4 5 6
A 1
B 2
C 3
D 4 #-> This is what's currently happening
任何帮助将非常感激。谢谢!
df.iloc[location].to_csv(path,mode='a')
这是我一直在尝试的代码。
解决方案
df.iloc[location]
可以给你Series
哪些不同的待遇DataFrame
您必须将其转换为DataFrame
但使用列表Series
来获取行而不是列中的数据
df = pd.DataFrame( [df.iloc[location]] )
然后保存到没有标题的csv
df.to_csv(path, mode='a', header=None)
编辑:您也可以转换Series
为DataFrame
使用.to_frame()
,然后使用.T
将列转置为行
df = df.iloc[location].to_frame().T
推荐阅读
- dart - dart 中模型的简单可选类型
- javascript - 如何通过 Angular 中的路由在 Cloud Functions 中执行对 Cloud Firestore 的添加?
- ios - 在 UITableView 中至少选择一行
- ios - 从 AppDelegate 调用一个函数并使用该函数执行 segue
- wordpress - 更改“/shop”档案中的程序默认产品变体图像-Woocommerce
- r - 为什么我的 dplyr 语句会创建额外的行?
- c# - 使用通用扩展方法将实体映射到视图模型
- vba - VBA更改文件夹中所有文件的名称
- c# - 实体框架延迟加载总和子表慢
- c# - 使用锁来排队任务是一个好习惯吗?