Home > Mobile >  Creating DataFrames from cell ranges to create an output
Creating DataFrames from cell ranges to create an output

Time:09-01

Here is my code:

import pandas as pd
import os


 data_location = ""
os.chdir(data_location)

df1 = pd.read_excel('Calculation - (Vodafone) July 22.xlsx', sheet_name='PPD Summary', 
index_col=False)
df2 = df1.iat[3, 5]
df3 = df1.iat[4, 5]
df4 = '9999305'
df5 = df1.iat[3, 1]
df6 = df1.iat[4, 1]
df7 = df1.iat[3, 6]
df8 = df1.iat[4, 6]

print(df4, df5, df2, df7)
print(df4, df6, df3, df8)

Running this script will return me the following which I want to output to a csv:

9999305 0.007018639425878576 GB GBP
9999305 0.006709984038878434 IE EUR

The cells which contain the information I need are in B5:B6, F5:F6 & G5:G6. I have tried using openpyxl to get the cell ranges, however I am struggling to present and output these in a way so that csv that is outputted like the above.

CodePudding user response:

Try:

result = pd.DataFrame([[df4, df5, df2, df7],
                       [df4, df6, df3, df8]])
result.to_csv('filename.csv', header=False, index=False)

'filename.csv' will contain:

9999305,0.007018639425878576,GB,GBP
9999305,0.006709984038878434,IE,EUR

If you want just to print them in a comma-separated-format:

print(df4, df5, df2, df7, sep=',')
print(df4, df6, df3, df8, sep=',')
  • Related