Python 如何打印数据框的每一行,包括列名?
我的数据框架设置如下:Python 如何打印数据框的每一行,包括列名?,python,pandas,printing,iteration,Python,Pandas,Printing,Iteration,我的数据框架设置如下: Project CriteriaOne CriteriaTwo CriteriaThree Proj A Comments A Comments B Comments C Proj B Comments D Comments E Comments F Proj C Comments G Comments H Comments I 我想以以下格式写入文件: Proj A Criteria One: Comments A Criteria
Project CriteriaOne CriteriaTwo CriteriaThree
Proj A Comments A Comments B Comments C
Proj B Comments D Comments E Comments F
Proj C Comments G Comments H Comments I
我想以以下格式写入文件:
Proj A
Criteria One: Comments A
Criteria Two: Comments B
Criteria Three: Comments C
Proj B
Criteria One: Comments D
Criteria Two: Comments E
Criteria Three: Comments F
Proj C
Criteria One: Comments G
Criteria Two: Comments H
Criteria Three: Comments I
我已经使用enumerate(是的,我知道它不“正确”)、iterrows(是的,我知道它很慢)和itertuples处理了大量代码片段,虽然我可以在每个代码片段中获得部分成功,但我觉得我忽略了一些显而易见的东西
理想情况下,我不会按名称引用“CriteriaOne”,因为我正在转换使用相同格式的两个不同的东西,但“CriteriaOne”将在另一个文件中命名为“FlarbleOne”
我已经完成了其他部分(使用groupby合并多行,以及必要的文件处理),但我还没有完全理解这一重要部分。迭代此数据帧并在输出中对内容进行良好标记的优雅方式是什么?假设您的数据帧是
f = pd.DataFrame({'A': {0: 'a', 1: 'b', 2: 'c'},'B': {0: 1, 1: 3, 2: 5},'C': {0: 2, 1: 4, 2: 6}})
输出:
A B C
0 a 1 2
1 b 3 4
2 c 5 6
只是像这样迭代
for i in f.index:
print()
print(i)
for j in f.columns:
print(f'{j} : {f.iloc[i][j]}')
将输出为
0
A : a
B : 1
C : 2
1
A : b
B : 3
C : 4
2
A : c
B : 5
C : 6
我们可以在每一行进行迭代。我假设你想把它写在txt文件中 数据帧创建:
import pandas as pd
col = ["Project", "CriteriaOne", "CriteriaTwo", "CriteriaThree"]
data = [["Proj A", "Comments A", "Comments B", "Comments C"],
["Proj B", "Comments D", "Comments E", "Comments F"],
["Proj C", "Comments G", "Comments H", "Comments I" ]]
df = pd.DataFrame(data, columns=col)
Project CriteriaOne CriteriaTwo CriteriaThree
0 Proj A Comments A Comments B Comments C
1 Proj B Comments D Comments E Comments F
2 Proj C Comments G Comments H Comments I
Proj A
CriteriaOne: Comments A
CriteriaTwo: Comments B
CriteriaThree: Comments C
Proj B
CriteriaOne: Comments D
CriteriaTwo: Comments E
CriteriaThree: Comments F
Proj C
CriteriaOne: Comments G
CriteriaTwo: Comments H
CriteriaThree: Comments I
数据帧:
import pandas as pd
col = ["Project", "CriteriaOne", "CriteriaTwo", "CriteriaThree"]
data = [["Proj A", "Comments A", "Comments B", "Comments C"],
["Proj B", "Comments D", "Comments E", "Comments F"],
["Proj C", "Comments G", "Comments H", "Comments I" ]]
df = pd.DataFrame(data, columns=col)
Project CriteriaOne CriteriaTwo CriteriaThree
0 Proj A Comments A Comments B Comments C
1 Proj B Comments D Comments E Comments F
2 Proj C Comments G Comments H Comments I
Proj A
CriteriaOne: Comments A
CriteriaTwo: Comments B
CriteriaThree: Comments C
Proj B
CriteriaOne: Comments D
CriteriaTwo: Comments E
CriteriaThree: Comments F
Proj C
CriteriaOne: Comments G
CriteriaTwo: Comments H
CriteriaThree: Comments I
代码
with open("./temp.txt", "w") as file:
for _, row in df.iterrows():
file.write(row[0]+"\n")
for key, value in row[1:].items():
file.write(key+": "+value+"\n")
file.write("\n")
输出:
import pandas as pd
col = ["Project", "CriteriaOne", "CriteriaTwo", "CriteriaThree"]
data = [["Proj A", "Comments A", "Comments B", "Comments C"],
["Proj B", "Comments D", "Comments E", "Comments F"],
["Proj C", "Comments G", "Comments H", "Comments I" ]]
df = pd.DataFrame(data, columns=col)
Project CriteriaOne CriteriaTwo CriteriaThree
0 Proj A Comments A Comments B Comments C
1 Proj B Comments D Comments E Comments F
2 Proj C Comments G Comments H Comments I
Proj A
CriteriaOne: Comments A
CriteriaTwo: Comments B
CriteriaThree: Comments C
Proj B
CriteriaOne: Comments D
CriteriaTwo: Comments E
CriteriaThree: Comments F
Proj C
CriteriaOne: Comments G
CriteriaTwo: Comments H
CriteriaThree: Comments I
想法是将
项目
列转换为索引
,并通过字典循环创建,向系列
添加值,并附加到文件头:
df = df.set_index('Project')
with open('file.csv', 'a') as file:
for i, j in df.T.to_dict(orient='series').items():
s = j.index + ': ' + j
file.write(f'{i}\n')
s.to_csv(file, header=False, index=False, line_terminator='\n')
file.write('\n')
这很有效——我在解决类型以使其正常工作和解决其他一些问题时遇到了一些问题,但这使得调整最终格式变得很容易,并且在我阅读时很有意义(尤其是对于曾经涉猎perl的人)。谢谢我喜欢这里的哲学方法。现在,我已经让它在上面工作,我想回去玩这个,因为它感觉像是一个更简单的模型。谢谢