Home > Software design >  Pandas: Printing all the values from an index
Pandas: Printing all the values from an index

Time:10-18

I am calculating concentrations from a file that has separate Date and Time columns. I set the date and time columns as indexes as they are not suppose to change. However when I print the new dataframe it only prints the "date" one time like this:

                      55... 
Date       Time                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
2020-12-30 10:37:04  0.000000 ...
           10:37:07  0.000000 ...
           10:37:10  0.000000 ...
           10:37:13  0.000000 ...
           10:37:16  0.000000 ...

What I need is for it to print the date for each row just like it is in the original dataframe. It should remain the same as the original dataframe date and time values. The original dataframe is:

Date    Time    Accum.  Scatter...
2020-12-30  10:37:04    3   0.789...
2020-12-30  10:37:07    3   0.814...
2020-12-30  10:37:10    3   0.787...
2020-12-30  10:37:13    3   0.803...
2020-12-30  10:37:16    3   0.798...
2020-12-30  10:37:19    3   0.818...
2020-12-30  10:37:22    3   0.809...

The code I have is :

df = pd.read_csv('UHSAS 20201230c.txt',delimiter=r'\s ',engine = 'python')
df.set_index(['Date', 'Time'],inplace=True)

concentration = df.iloc[:,13:].div(df.Sample,axis=0)

print(concentration.to_string())

I know it seems simple but I am new to pandas. Thank you in advance.

CodePudding user response:

From the Pandas to_string() docs.

sparsify : bool, optional, default True
Set to False for a DataFrame with a hierarchical index to print every multiindex key at each row.


print(concentration.to_string(sparsify=False))

import pandas as pd
d = {'a':[1,1,1,3,5,6],
     'b':[1,2,3,4,5,6],
     'c':[9,8,7,6,5,4]}
df = pd.DataFrame(d)
df = df.set_index(['a','b'])

>>> print(df.to_string())
     c
a b   
1 1  9
  2  8
  3  7
3 4  6
5 5  5
6 6  4

>>> print(df.to_string(sparsify=False))
     c
a b   
1 1  9
1 2  8
1 3  7
3 4  6
5 5  5
6 6  4

CodePudding user response:

remove inplace=True and replace it with reset_index() at the end

here is the code (first two lines)

df = pd.read_csv('csv2.txt',delimiter=r'\s ',engine = 'python')
df.set_index(['Date', 'Time'] ).reset_index()

# cannot do calculation step, as sample column is not present in the sample data
Date    Time    Accum.  Scatter...
0   2020-12-30  10:37:04    3   0.789...
1   2020-12-30  10:37:07    3   0.814...
2   2020-12-30  10:37:10    3   0.787...
3   2020-12-30  10:37:13    3   0.803...
4   2020-12-30  10:37:16    3   0.798...
5   2020-12-30  10:37:19    3   0.818...
6   2020-12-30  10:37:22    3   0.809...
  • Related