0

I would like to plot amount of columns for 2 different scenario based on index of rows in my dataset preferably via Pandas.DataFrame :

1st scenario: columns index[2,5,8,..., n+2]

2nd scenario: the last 480 columns or column index [961-1439] img picture

I've tried to play with index of columns which is following:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

dft = pd.read_csv("D:\Test.csv" , header=None)
dft.head()

id_set = dft[dft.index % 2 == 0].astype('int').values
A = dft[dft.index % 2 == 1].values
B = dft[dft.index % 2 == 2].values
C = dft[dft.index % 2 == 3].values
data = {'A': A[:,0], 'B': B[:,0], 'C': C[:,0]}
df = pd.DataFrame(data, columns=['A','B','C'], index = id_set[:,0])

#1st scenario
j=0
index=[]
for i in range(1439):
    if j==2:
        j=0
        continue
    else:
        index.append(i)
        j+=1
print(index)

#2nd scenario
last_480 = df.[0:480][::-1]

I've found this post1 and post2 but they weren't my case!

I would appreciate if someone can help me.

Mario
  • 1,631
  • 2
  • 21
  • 51

1 Answers1

2

1st scenario:

df.iloc[:, 2::3]

The slicing here means all rows, columns starting from the 2nd, and every 3 after that.

2nd scenario:

df.iloc[:, :961:-1]

The slicing here means all rows, columns to 961 from the end of the list.

EDIT:

import matplotlib.pyplot as plt
import seaborn as sns

senario1 = df.iloc[:, 2::3].copy()
sns.lineplot(data = senario1.T)

You can save the copy of the slice to another variable, then since you want to graph row-wise you need to take the transpose of the sliced matrix (This will make yours rows into columns).

Ben Pap
  • 2,549
  • 1
  • 8
  • 17
  • man thanks for response but do you know how can I plot selected slices based on row indexes with different colors? for example in 1st scenario? selected columns belong to 1st row(Row index=0 with red ) , selected columns belong to 2nd row(Row index=1 with green ) and so on... – Mario Apr 08 '19 at 12:41
  • Added how you would graph it, let me know if you have any more questions! – Ben Pap Apr 09 '19 at 18:46
  • Hi it seems something is wrong I've got an error `ValueError: These style levels are missing dashes: {6, 7, 8, .., 39}`. can you fix it? – Mario Apr 10 '19 at 10:54
  • 1
    sns.lineplot(data = senario1.T, dashes = False) should fix it. – Ben Pap Apr 10 '19 at 17:09
  • I inspired by your comment and found `sns.scatterplot(data = senario1, markers = False, legend=False)` was OK but I couldn't find good `cmap`. I realized `sns.lineplot()` doesn't give me my interested plot. – Mario Apr 10 '19 at 18:50
  • I also have another [question](https://stackoverflow.com/questions/55270346/how-can-fit-the-data-on-temperature-thermal-profile?) about slice of data and plot and fit them if you're interested. – Mario Apr 10 '19 at 18:55