how to create a pandas dataframe out of the return value of psutil.net_io_counters - dictionary

I looked all over to format the output of returned values from the psutil.net_io_counters(pernic=True).
I have the values...but it looks like each interface is the key, and the value is a tuple of 8 different values.
I want pandas to list the interface as a column and then create a second column where each item in the tuple is given a row beneath that second column.
any ideas?

Related

How to split rows within a dataframe for a target column with multiple/nested values

With a dataframe that has, for example, one column x that has nested or multiple values for some rows, how would i, for those rows that have multiple values for x, append duplicate rows to the dataframe, save that that they correspond to one value within x.
To try to explain better, see "mock dataframe pre-transform", below. Row 1 has values "webui, cli, mobile" for column "module", and what i want is to append three near copies of row 1 to the dataframe, one with module value "webui", one with module value "cli" and one with module value "mobile". I also then want to remove the the original row 1. A similar operation would occur for row 4, such that the final dataframe would have 7 rows (see "mock dataframe post-transform, below).
mock dataframe pre-transform
mock dataframe post-transform

How to choice specific data value by row or column index in SAS?

I usually use R, but I just start studying SAS.
In R, we can make some data.frame like this :
df <- as.data.frame(matrix(c(1:6),nrow=2,ncol=3))
and then
df[1,2]
is 3.
Here is my question. How can I use row and column index in SAS?
I coudln't find this..
I want to use row and column number by index of double loop
If the row number and column number have meaning then you probably do not want to store your "matrix" in that form. Instead you probably want to store it in a tall format where the row and column values are stored in variables and the values of the cells in your matrix are stored in another variable. Since you didnt' provide any meaning to your example let's just name these variables ROW, COL and VALUE.
data have;
do col=1 to 3 ;
do row=1 to 2 ;
value+1;
output;
end;
end;
run;
Now if you want to find the value when ROW=1 and COL=2 it is a simple WHERE condition.
proc print data=have;
where row=1 and col=2;
run;
Result:
Obs col row value
3 2 1 3
In a real dataset the ROW might be the individual case or person ID and the COL might be the YEAR or VISIT_NUMBER or SAMPLE_NUMBER of the value.
You access columns via names and rows via _n_ if you really need but there isn't a good usage for this type of logic.
For example, if you wanted the third row and second variable from the SASHELP CLASS data set.
Note that you need to know the name of the variable, you cannot rely on the index/position.
This displays the information:
proc print data=sashelp.class(firstobs = 2 obs=2);
var Age;
run;
This puts in a data set, two different ways:
data want;
set sashelp.class (firstobs = 2 obs=2);
keep age;
run;
data want;
set sashelp.class ;
if _n_ = 3; *filters only the third row into the data set;
run;
R & Python import all data into memory and use that method as the default processing method. SAS instead only loads one row at a time and then continues to loop through each row within a data step, so you have to think of each step differently. Basically break your processes into smaller steps and they work. SAS does have some really nice built in functionality, like confidence intervals by default or the ability to aggregate data at multiple levels within a single procedure.

How do I select specific vectors of a matrix to be plotted against each other (such as when using hexplom)?

Is there a quick way to code for those specific vectors? Like I only want to use every 4th column in my matrix then plot the selected columns. I'm very new to R and have absolutely no idea what I'm doing. I know how to select a single vector and how to select a certain number in a row but that doesn't really help.
If you're looking to extract every 4th column from a matrix you can use seq().
Here's an example. I made a dummy dataset: foo<-matrix(c(rep(c(4,3,2,7),100)),nrow=10,ncol=10)
Then you can store the column indexes that you want from your matrix like so:
colsyouwant<-seq(from = 4, to = ncol(foo), by = 4)
from = whatever column you'd like to start from, in your case the 4th. Then you specify where you'd like to stop, so I used the ncol function to count how many columns are in the matrix. In this case my matrix isn't a multiple of 4 but it doesn't matter because seq stops before then. Then by=4 because you want to select every fourth column.
The colsyouwant now equals to 4 8. Simply use brackets and the name of your variable to get the columns you want out. foo[,colsyouwant]. Here the brackets just specify what part of the matrix I want as an output, it goes [rows,columns]. Since I want all the rows I leave that spot blank and then specify the rows using the colsyouwant variable, or in other words 4 8.

Naming the number of the row in a data frame that contains a certain value

I've done some thorough research and I am struggling with an attempt to find a function that will name the number of the row (in my data frame the rows don't contain numbers) that contains a certain value. In this case a number.
e.g. Call the data frame = df
I don't know how to show a little image of the data frame but say that in row 5, column 4 the value was '162', is there a function I could use that will end with the return being '5' or 'row 5'?
I have used rowsums(df=="162")
which gives a long line of the rows, if they contain the values there is a '1' under them, if not a '0' but I need a function that simply states the row.
I couldn't figure out how to correctly use the 'which' function either.
which(df$col4=='162')
I am assuming that col4 is the name of the column number 4

extract columns that don't have a header or name in R

I need to extract the columns from a dataset without header names.
I have a ~10000 x 3 data set and I need to plot the first column against the second two.
I know how to do it when the columns have names ~ plot(data$V1, data$V2) but in this case they do not. How do I access each column individually when they do not have names?
Thanks
Why not give them sensible names?
names(data)=c("This","That","Other")
plot(data$This,data$That)
That's a better solution than using the column number, since names are meaningful and if your data changes to have a different number of columns your code may break in several places. Give your data the correct names and as long as you always refer to data$This then your code will work.
I usually select columns by their position in the matrix/data frame.
e.g.
dataset[,4] to select the 4th column.
The 1st number in brackets refers to rows, the second to columns. Here, I didn't use a "1st number" so all rows of column 4 are selected, i.e., the whole column.
This is easy to remember since it stems from matrix calculations. E.g., a 4x3 dimensional matrix has 4 rows and 3 columns. Thus when I want to select the 1st row of the third column, I could do something like matrix[1,3]

Resources