1

I want to be able to create a cross table/table/dataframe (what ever the name) like this:

____________________      
Performance  "value" (This value must come from a X vector, which has a formula to go to dataset, calculate and return this value)
____________________
LTFU         "value" (This value must come from a y vector, which has a formula to go to dataset, calculate and return this value)
____________________

Please, note that Performance and LTFU values are generated from a function applied to a .csv dataset in python. Performance and LTFU don't exist in the .csv dataset, both should be created just to allow me do a summary of performance.

What I get now is as below:

import pandas as pd
performance=pd.read_csv("https://www.dropbox.com/s/08kuxi50d0xqnfc/demo.csv?dl=1")

x=performance["idade"].sum()
y=performance["idade"].mean()

l = "Performance"
k = "LTFU"

def test(y):
return pd.DataFrame({'a':y, 'b':x})

test([l,k])

         a        b
0   Performance   x vector value here (it shows 1300, it is correct)
1   LTFU          y vector value here (it shows 1300, it is wrong, it should show 14.130434782608695 instead, according to the instruction of y vector)

You can copy and paste the above code to your python IDE and test and then return with your solution to me. Please, show me an example with the table results as I want.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
MGB.py
  • 461
  • 2
  • 9
  • 25
  • My text has beeen distorced here. I am posting a screenshsot – MGB.py Feb 11 '18 at 09:48
  • What are you trying to do? Are you trying to get the same format to save as CSV / txt? Or are you trying to summarize this dataframe to reuse? – Deena Feb 11 '18 at 11:50
  • @Deena, I am trying to summarize this dataframe with new variables. What I want is to calculate the those 2 values from another variables within dataset. I want to get new values generated by another calculation. Please, note that Perfomance and LTFU dont exist in the csv dataset.They are new variables just created to summarize want I want. – MGB.py Feb 11 '18 at 12:13
  • I am confused `test([l,k])` return `DataFrame`. So need write it to file? Or need create another DataFrame from `csv` - `Performance 1300 ____________________ LTFU 60`, add it to `test([l,k])` and write back? – jezrael Feb 12 '18 at 09:39
  • Yeah, Jezrael. I need to create another table(or dataframe whatever the name) which contains Performance value that is correct 1300 according to my function above and also must contain LTFU value (which must not be 1300 because the function which generate this value is different from Performance). Did you get it? – MGB.py Feb 12 '18 at 09:44
  • @jezrael check it know after my edition, looks like clear now. – MGB.py Feb 12 '18 at 09:53
  • so need `df1 = test([l,k])` and `df1.to_csv('file.csv', index=False, header=None)` ? – jezrael Feb 12 '18 at 09:55
  • test([l,k]) generates a table that I want but doesnt bring the value from y=performance["idade"].mean(). It only brings me the correct value of x=performance["idade"].sum(). – MGB.py Feb 12 '18 at 10:15
  • @jezrael, what does this code: df1.to_csv('file.csv', index=False, header=None) ??? – MGB.py Feb 12 '18 at 10:29
  • No need to do that, I just want to create this simple table with Performance and LTFU values. Those values must be calculated from some variables present in the dataset. Only this !!! I dont know why I cant explain well to you. For me it looks like simple. – MGB.py Feb 12 '18 at 10:37
  • OK, why `df1.to_csv('file.csv', index=False, header=None)` does not work? Or need `df1.to_csv('file.csv', index=False, header=None, sep=' ')` ? – jezrael Feb 12 '18 at 11:26
  • @jezrael, sorry I did not try it. Ok, I will try it and let you know but i must confess that I dont know if we are trying to solve the same question. Thanks. – MGB.py Feb 12 '18 at 12:09
  • @jezrael, sorry I did not try it. Ok, I will try it and let you know but i must confess that I dont know if we are trying to solve the same question. Thanks. – MGB.py Feb 12 '18 at 12:09
  • OK, I understand it you need write dataframe to file, correct? – jezrael Feb 12 '18 at 12:10
  • It seems like that. I am new to Python and some terminology is not quite familiar to me. Some time I just do things without knowing the real names or terminology of what I am doing. Sorry for that. Once I see your result I can assure you if that was want I wanted. Thank so much for your attention. – MGB.py Feb 12 '18 at 12:25

2 Answers2

0

your requirement does not fit the definition of pandas data frame, you already have the values, so may be you can use output using other ways

Manjit Ullal
  • 106
  • 8
  • Looks like you did not understand my concern. Yesterday I have edited my question to be more clear to you. **you already have the values, so may be you can use output using other ways** - Which values? The second below value of 1300 on generated above table should not appear there, it is a replication of the above 1300 which is correct according to the function(formula) I have created. – MGB.py Feb 12 '18 at 08:58
0

You need assign output to DataFrame and then write to file by DataFrame.to_csv:

l = "Performance"
k = "LTFU"

#changed input to 2 scalar values
def test(l1,k1):
    #changed a to list [l1, k1]
    #changed b to list [x, y]
    return pd.DataFrame({'a':[l1, k1], 'b':[x, y]})

df1 = test(l,k)
print (df1)
             a            b
0  Performance  1300.000000
1         LTFU    14.130435
df1.to_csv('file.csv', index=False, header=None, sep=' ')
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • your code writes to a .csv and gives me the same wrong result as per vector y formula. I dont want to write to .csv the output. I just want that simple table as output only that but you need to correct to let y value be correct and not replicate x vector formula. Is there anyone who understands what I want, please, help me! I need that table output to be in DASH python Dashboard. – MGB.py Feb 12 '18 at 18:28
  • @MGB.py I have few question. 1. `df1 = test([l,k])` `print(df1)` . This create dataframe, (table) generate from data from `demo.csv`. What need to do? Display only? Or something else? 2. Why are incorrect values in `df1`? If `demo.csv` contains only data from question, what is expected output? (I cannot see all data in file, so I try simplify it). 3. What is input to DASH python Dashboard? Dataframe? Dictionary? Something else? Please be patient, I try to help you. But your thinking and my is different, so it seems we dont understand each other. Thank you. – jezrael Feb 12 '18 at 19:03
  • I am editing the question showing to you all demo dataset to let we talk same language. Wait. – MGB.py Feb 12 '18 at 19:20
  • @MGB.py Sure. I have main problem why `df1 = test([l,k])` `print(df1)` is not your expected output. Because column 0, 1 and a, b first row? Or somwthing else? – jezrael Feb 12 '18 at 19:36
  • check again the updated question, i have shared demo csv dropbox url and commented on the table below.Thanks. – MGB.py Feb 12 '18 at 20:12
  • Cant assign your name here with @, @jazrael, check the question now. – MGB.py Feb 12 '18 at 20:14
  • sorry I did not understand you. Check what? Have you fixed my problem? – MGB.py Feb 13 '18 at 10:00
  • Yes, there was problem in function. – jezrael Feb 13 '18 at 10:01
  • wow thanks for your solution. Can I ask more just to customize the table? I want to remove letters "a" and "b" and I think with I paste your code to Dash python Dashboard the numbers "0" and "1" should disappear.Am I right?. Can you do it? – MGB.py Feb 13 '18 at 10:17
  • I will share with you a picture of customized tables that I want. Thank you very much to be patient with my problem. – MGB.py Feb 13 '18 at 10:19
  • @MGB.py - What think `Dash python dashboard` ? It is [this](https://plot.ly/products/dash/) or something else? – jezrael Feb 13 '18 at 10:19
  • dash is python dashboard similar to shiny R dashboard – MGB.py Feb 13 '18 at 10:29
  • Not sure, if it is possible [link](https://stackoverflow.com/search?q=%5Bplotly-dash%5D+pandas) – jezrael Feb 13 '18 at 10:35
  • I ve sent a table picture for your email.Check it. – MGB.py Feb 13 '18 at 10:37
  • Look this post https://stackoverflow.com/questions/8356501/python-format-tabular-output and table and see the table proposed by @TommasoF. Is like that what i want.Thanks – MGB.py Feb 13 '18 at 10:43
  • Yes, there is a [lot solutions](https://stackoverflow.com/a/26937531/2901002) what do you prefer? – jezrael Feb 13 '18 at 11:32
  • But the best is some solution from [this](https://stackoverflow.com/q/18528533/2901002) – jezrael Feb 13 '18 at 11:37
  • Not really because I dont want to appear column names, just row names and values. :-) – MGB.py Feb 13 '18 at 12:04
  • @MGB.py - hmmm, so it is too broad question. Do you need html table? Or something else? Obviously is dataframe generated to `csv` or `json` and then `csv` or `json` is display some other system as python/pandas. – jezrael Feb 13 '18 at 12:30
  • forgot about html, csv, json. I just want that table generated within python environment jupyter notebook, DASH, command line only.Thanks – MGB.py Feb 13 '18 at 13:30
  • DASH is already html associated so if I can do that table within core python it should be easily integrated in DASH. – MGB.py Feb 13 '18 at 13:32
  • Sorry, then I have no idea, because never working with it. And it seems it is not implemented in python (I cannot find some samples how do it.) – jezrael Feb 13 '18 at 13:33
  • I will share with you a full Dash command template but you dont need it. If you are able to do a table in python it should work also in Dash in almost similar way. – MGB.py Feb 13 '18 at 13:35
  • that is funny, look your answers to almost similar question I have on this post: https://stackoverflow.com/questions/36688022/removing-header-column-from-pandas-dataframe – MGB.py Feb 14 '18 at 11:08
  • oooo, it is really super ;) So you get soluion ;) And from me, it is really funny ;D – jezrael Feb 14 '18 at 11:09
  • you gave a solution for removing column name here: https://stackoverflow.com/questions/44917675/pandas-delete-column-name df.columns = [''] * len(df.columns) – MGB.py Feb 14 '18 at 11:19
  • Yes, exactly ;) – jezrael Feb 14 '18 at 11:23
  • 1
    so you had forgot what you did before. It happens!Ok. Thanks. – MGB.py Feb 14 '18 at 11:40