I am working on below code to write a list of data to a CSV file with python I am using below code.
import pandas as pd
df = pd.read_csv ("input.csv")
#From column name Test2 take count value of entry which is simple
simp = df.Test2.value_counts().Simple
list1 = [2, 5, 3, 3, 3]
#multiplying the above list value with the count of simple from input CSV
simplelist = [i * simp for i in list1]
print(simplelist)
# till here its working. Its printing as expected [24, 60, 36, 36, 36]
# I need to write the CSV with above value 1 to 5 from above list with Type as Simple
df1 = pd.DataFrame[(simplelist)]
index=['Simple', '0']
columns=['Type', 'Value1', 'Value2', 'Value3', 'Value4', 'Value5']
df1.to_excel("output.xlsx")
Above code is failing, request you to help to address the same
You are not calling the index
and column
variable anywhere in the script.
Just define the index
and column
lists where you create the DataFrame
.
import pandas as pd
df = pd.read_csv ("input.csv")
#From column name Test2 take count value of entry which is simple
simp = df.Test2.value_counts().Simple
list1 = [2, 5, 3, 3, 3]
#multiplying the above list value with the count of simple from input CSV
simplelist = [i * simp for i in list1]
print(simplelist)
df1 = pd.DataFrame(simplelist, index=['Simple', '0'], columns=['Type', 'Value1', 'Value2', 'Value3', 'Value4', 'Value5'])
df1.to_excel("output.xlsx")
I think you need create list with +
for join and pass to DataFrame
constructor:
simplelist = [24, 60, 36, 36, 36]
columns=['Type', 'Value1', 'Value2', 'Value3', 'Value4', 'Value5']
df1 = pd.DataFrame([['Simple'] + simplelist], columns=columns)
print (df1)
Type Value1 Value2 Value3 Value4 Value5
0 Simple 24 60 36 36 36
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.