在csv文件中打印多行输出

时间:2019-05-31 08:51:39

标签: python pandas csv sorting printing

假设

.*

实际输出:

它仅显示最后一个输出,而不是每个值。像这样,如果有N个值,则csv必须将所有N个值都存储在彼此之间一个

A='First'
B='Random'
C='Degree'
D='Largest'

A='Second'
B='Odd'
C='Inclined'
D='Maximum'

A='Third'
B='Even'
C='Steep'
D='Smallest'

A='Fourth'
B='Prime'
C='Gradient'
D='Minimum'

c = ['Group', 'Number', 'Angle', 'Max value']

df = pd.DataFrame([[A, B, C, D]], columns=c)
print (df)

#to csv
df.to_csv('Output.csv', encoding='utf-8', index=False)

预期输出:

Output

5 个答案:

答案 0 :(得分:1)

每次重新定义变量时,您都将覆盖A B C D变量。您应该将它们添加到嵌套列表或使用循环:

data = [
    ['First', 'Random', 'Degree', 'Largest'],
    ['Second', 'Odd', 'Inclined', 'Maximum'],
    ['Third', 'Even', 'Steep', 'Smallest'],
    ['Fourth', 'Prime', 'Gradient', 'Minimum']
]

df = pd.DataFrame(data, columns=c)

或者....

A = ['First', 'Random', 'Degree', 'Largest']
B = ['Second', 'Odd', 'Inclined', 'Maximum']
C = ['Third', 'Even', 'Steep', 'Smallest']
D = ['Fourth', 'Prime', 'Gradient', 'Minimum']

df = pd.DataFrame([A, B, C, D], columns=c]

答案 1 :(得分:1)

您可以使用以下代码,

import pandas as pd

data = [
    ['First', 'Random', 'Degree', 'Largest'],
    ['Second', 'Odd', 'Inclined', 'Maximum'],
    ['Third', 'Even', 'Steep', 'Smallest'],
    ['Fourth', 'Prime', 'Gradient', 'Minimum']
]


c = ['Group', 'Number', 'Angle', 'Max value']

df = pd.DataFrame(data, columns=c)
print (df)

df.to_csv('Output.csv', encoding='utf-8', index=False)

,输出为

enter image description here

您还获得了CSV文件

enter image description here

答案 2 :(得分:1)

您每次都覆盖变量A, B, C, D,所以最后in只包含您上次迭代的值。

您的变量的构造有点直观,但是以下情况适用于您:

A = ['First', 'Second', 'Third', 'Fourth']
B = ['Random', 'Odd', 'Even', 'Prime']
C = ['Degree', 'Inclined', 'Steep', 'Gradient']
D = ['Largest', 'Maximum', 'Smallest', 'Minimum']

c = ['Group', 'Number', 'Angle', 'Max value']

df = pd.DataFrame(data =[A, B, C, D])

df = df.T
df.columns = c
print (df)

#to csv
df.to_csv('Output.csv', encoding='utf-8', index=False)

    Group  Number     Angle Max value
0   First  Random    Degree   Largest
1  Second     Odd  Inclined   Maximum
2   Third    Even     Steep  Smallest
3  Fourth   Prime  Gradient   Minimum

答案 3 :(得分:1)

您应遵循以下步骤:

首先列出所有列,如下所示:

A = ['First', 'Random', 'Degree', 'Largest']
B = ['Second', 'Odd', 'Inclined', 'Maximum']
C = ['Third', 'Even', 'Steep', 'Smallest']
D = ['Fourth', 'Prime', 'Gradient', 'Minimum']

然后

c = ['Group', 'Number', 'Angle', 'Max value']
df = pd.DataFrame([A, B, C, D], columns=c)
print (df)

#to csv
df.to_csv('Output.csv', encoding='utf-8', index=False)

输出:

    Group  Number     Angle Max value
0   First  Random    Degree   Largest
1  Second     Odd  Inclined   Maximum
2   Third    Even     Steep  Smallest
3  Fourth   Prime  Gradient   Minimum

答案 4 :(得分:1)

尝试此代码非常简单

dictionary = {'Group':['First','Second','Third','Fourth'],
'Number' :['Random','Odd','Even','Prime'],
'Angle':['Degree','Inclined','Steep','Gradient'],
'Max value' :['Largest','Maximum','Smallest','Minimum']}
df = pd.DataFrame(dictionary)
print (df)
#to csv
df.to_csv('Output.csv', encoding='utf-8', index=False)