1

I am writing a model. I need help.

The last part of my code is;

x_irp_sum_eliminated_df = pd.DataFrame(x_irp_sum_eliminated).T

print(x_irp_sum_eliminated_df.head())

Output is;

output

Question:

How can I run this output in which every column will correspond to a line?

I want an output like that;

x1_3_1 + x1_4_1 + x1_5_1 + x1_9_1 < 1

x1_7_3 < 1

x1_1_4 + x1_3_4 + x1_9_4 < 1

.....

woblob
  • 1,349
  • 9
  • 13
Harun Horasanlı
  • 145
  • 1
  • 1
  • 8
  • [Please do not upload images of code/errors/data when asking a question.](//meta.stackoverflow.com/q/285551) Doing so makes it impossible to copy/paste and forces potential answerers to type out your code/data to reproduce your problem. Include these as a [formatted code block](//stackoverflow.com/help/formatting) instead of an image. [Why do we hate screenshots so much?](//meta.stackoverflow.com/q/303812/) – Pranav Hosangadi Oct 13 '20 at 18:31
  • Thanks for kindly correction Pranav. I will be careful about this. – Harun Horasanlı Oct 13 '20 at 19:04

1 Answers1

1

Your dataframe has N rows and M columns. You want to join all values in each column with " + ". All you need to do is " + ".join(df[colname]). To get rid of the None values, we can do df[colname].dropna() before joining. To make sure they're strings before we join, just map the str function on df[colname] before joining.

So to do this for each column:

for colname in df.columns:
    print(" + ".join(df[colname].dropna()) + " < 1")

Testing:

df = pd.read_csv(StringIO("0, 1, 2\nx1_3_1, x1_7_3, x1_1_4\nx1_4_1,       , x1_3_4\nx1_5_1,       , x1_9_4\nx1_9_1,       ,"), skipinitialspace=True)
df = df.where(pd.notnull(df), None)
print(df)

# These shenanigans to mock your data

# Output:
#         0       1        2
# 0  x1_3_1  x1_7_3   x1_1_4
# 1  x1_4_1    None   x1_3_4
# 2  x1_5_1    None   x1_9_4
# 3  x1_9_1    None     None


for colname in df.columns:
    print(" + ".join(df[colname].dropna()) + " < 1")
# Output:
# x1_3_1 + x1_4_1 + x1_5_1 + x1_9_1 < 1
# x1_7_3 < 1
# x1_1_4 + x1_3_4 + x1_9_4 < 1

To write to a file, and ignore empty columns:

with open('myfile.txt', 'w') as wf:
    for colname in df.columns:
        col = df[colname].dropna() # Drop nones
        if not col.empty: # Checking if column is not empty after dropping nones
           wf.write(" + ".join(df[colname].dropna()) + " < 1\n")

We only need to open the write handle once outside the loop.

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
  • Thanks for answer Pranav. I tried this. I have two more questions. I have full none columns. This code adds these columns as a " < 1 ". I do not want these full none columns in my output result. 1) How can I ignore this columns? 2) How can I run this code as a txt output file without corrupting this separate lines? – Harun Horasanlı Oct 13 '20 at 19:12
  • 1
    @HarunHorasanlı 1. Check that the column [isn't empty](https://stackoverflow.com/a/24652521/843953) after you `dropna()` 2. Replace `print` with [code to write to a file.](https://stackoverflow.com/a/6160082/843953) – Pranav Hosangadi Oct 13 '20 at 19:15
  • 1. After dropna() it deletes nones. It works good, but, for example, in my output file 1 and 48538 columns are all none file. The code adds these columns as a " < 1 " in a line. I do not want these output files. – Harun Horasanlı Oct 13 '20 at 19:36
  • 1
    @HarunHorasanlı IDK what you mean by "None file". Can you elaborate? If the value is `None` as shown in your screenshot and my answer, it should get dropped by `dropna()`. Check to see whether the column is empty after `dropna()`. If the column is empty after this, don't do the `join()`. When you `join()` the remaining values, the `None` values shouldn't be in the output. – Pranav Hosangadi Oct 13 '20 at 19:39