pandas: print all non-empty rows from a DataFrame - python

I have this data:
time-stamp ccount A B C D E F G H I
2015-03-03T23:43:33+0000 0 0 0 0 0 0 0 0 0 0
2015-03-04T06:33:28+0000 0 0 0 0 0 0 0 0 0 0
2015-03-04T06:18:38+0000 0 0 0 0 0 0 0 0 0 0
2015-03-04T05:36:43+0000 0 0 0 1 0 0 0 0 0 0
2015-03-04T05:29:09+0000 0 0 0 1 0 0 0 0 1 0
2015-03-04T07:01:11+0000 0 0 1 0 1 0 0 0 0 0
2015-03-03T15:27:06+0000 19 0 1 0 1 0 0 0 0 0
2015-03-03T15:43:38+0000 10 0 1 0 1 1 0 0 0 0
2015-03-03T18:16:26+0000 0 0 0 1 0 0 0 0 0 0
2015-03-03T18:19:48+0000 0 0 0 0 0 0 0 0 0 0
2015-03-03T18:20:02+0000 4 0 0 0 0 1 0 0 0 0
2015-03-03T20:21:55+0000 2 0 0 0 0 0 1 0 0 0
2015-03-03T20:37:36+0000 0 0 0 0 0 0 0 0 0 0
2015-03-04T03:03:51+0000 1 0 0 0 0 0 1 0 0 0
2015-03-03T16:33:04+0000 9 0 0 0 0 0 0 0 0 0
2015-03-03T16:18:13+0000 1 0 0 0 0 0 0 0 0 0
2015-03-03T16:34:18+0000 4 0 0 0 0 0 0 0 0 0
2015-03-03T18:11:36+0000 5 0 0 0 0 0 0 0 0 0
2015-03-03T18:24:35+0000 0 0 0 0 0 0 0 0 0 0
I want to slice all rows which have at least a single one ("1") in the columns A to I.
For the above data, the output will be:
time-stamp ccount A B C D E F G H I
2015-03-04T05:36:43+0000 0 0 0 1 0 0 0 0 0 0
2015-03-04T05:29:09+0000 0 0 0 1 0 0 0 0 1 0
2015-03-04T07:01:11+0000 0 0 1 0 1 0 0 0 0 0
2015-03-03T15:27:06+0000 19 0 1 0 1 0 0 0 0 0
2015-03-03T15:43:38+0000 10 0 1 0 1 1 0 0 0 0
2015-03-03T18:16:26+0000 0 0 0 1 0 0 0 0 0 0
2015-03-03T18:20:02+0000 4 0 0 0 0 1 0 0 0 0
2015-03-03T20:21:55+0000 2 0 0 0 0 0 1 0 0 0
2015-03-04T03:03:51+0000 1 0 0 0 0 0 1 0 0 0
We have ignored all the rows which don't have a "1" in any of the columns from A to I.

You could use any and boolean indexing to select only the rows that have at least one entry equal to 1:
df[(df.loc[:,['A','B','C','D','E','F','G','H','I']] == 1).any(axis=1)]
Referring to columns by label is somewhat tedious if you have a lot of them so you can use slicing to make things a little neater:
df[(df.loc[:, 'A':'I'] == 1).any(axis=1)]

a = open("a.txt",'r')
for line in a:
new = line.split(" ")
if "1" in new[1:]:
print line
OUTPUT:
2015-03-04T05:36:43+0000 0 0 0 1 0 0 0 0 0 0
2015-03-04T05:29:09+0000 0 0 0 1 0 0 0 0 1 0
2015-03-04T07:01:11+0000 0 0 1 0 1 0 0 0 0 0
2015-03-03T15:27:06+0000 19 0 1 0 1 0 0 0 0 0
2015-03-03T15:43:38+0000 10 0 1 0 1 1 0 0 0 0
2015-03-03T18:16:26+0000 0 0 0 1 0 0 0 0 0 0
2015-03-03T18:20:02+0000 4 0 0 0 0 1 0 0 0 0
2015-03-03T20:21:55+0000 2 0 0 0 0 0 1 0 0 0
2015-03-04T03:03:51+0000 1 0 0 0 0 0 1 0 0 0
2015-03-03T16:18:13+0000 1 0 0 0 0 0 0 0 0 0

Another solution assuming that all the values in the columns A to I are nonnegative
df[(df.drop(['time-stamp','ccount'], axis=1).sum(axis=1) > 0)]
Of course the dropping part, can be combined with the other solutions

Related

Python keeps updating wrong list

I'm trying to create a code to simulate the spread of something, via a 2D list of nxn structure. My issue is this: when I create a temp of my original list via temp = [*board], board[:], etc. it nonetheless updates both lists and instead of returning,
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
returns
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
my code is here:
def spread(board, iterations, size):
temp = board[:]
for iteration in range(iterations):
for x in range(size):
for y in range(size):
if board[x][y] == 1:
if x+1 < size:
temp[x+1][y] = 1
if x-1 >= 0:
temp[x-1][y] = 1
if y+1 < size:
temp[x][y+1] = 1
if y-1 >= 0:
temp[x][y-1] = 1
board = temp[:]
return board
and I called it via
new_board = spread(my_board, 1, 15)
This is programming 101. Remember, lists are stored in heap, with pointers to them.
So really the variable board points to the place in heap where that array is stored. When you assign temp to board, what you are doing is creating a new pointer which points to that same array. I suggest taking a look at this using python tutor: https://pythontutor.com/visualize.html#mode=edit
For example:
b = [1,2,3,4,5]
a = b
a[0] = 2
print(b)
will output
[2,2,3,4,5]
Try it out in python tutor and you'll see what's happening!
To solve your problem, create a deep copy
def deep_copy(board):
temp = []
for i in range(len(board)):
row_copy = []
for j in range(len(board[0])):
row_copy.append(board[i][j])
temp.append(row_copy)
return temp

updating element in a list where list is the value of a dictionary

I am trying to make a viterbi table with possibility of a specific word in a part of speech occurring inserted into a table where the rows represent all possible part of speeches and the rows are sequence of words in a sentence. I want to update the cell with corresponding the likelihood (TP*EP) that a word of a specific part of speech can occur at in a sentence. But I'm stuck on inserting these likelihoods into the dictionary list.
I'm using the following for insertion, but it does not work. Is there an alternative to changing a specific element in a list where the lists are values of a dictionary?
likelihood = EP * TP
q[possible_pos][j] = likelihood
When I use this, I still get a table of zeroes when I am expecting some values in the table to be updated to floats(likelihood)
for i in range(2):
for j in range(len(word_list[i])):
for pos in pos_list:
q[pos] = [0 for x in range(len(word_list[i]))]
try:
word = word_list[i][j].split("\t")[0]
pos = word_list[i][j].split("\t")[1]
for possible_pos in word_dict[word].keys():
try:
print(possible_pos)
if j == 0:
TP = Transition['Begin_Sent'][possible_pos]
EP = pos_dict[possible_pos][word]
elif word_list[i][j + 1] == ".\t.":
TP = Transition['End_Sent'][possible_pos]
EP = 1
else:
prev_pos = word_list[i][j - 1].split("\t")[1]
TP = Transition[prev_pos][possible_pos]
EP = pos_dict[possible_pos][word]
likelihood = EP * TP
q[possible_pos][j] = likelihood
except KeyError:
pass
except IndexError:
pass
df = pd.DataFrame.from_dict(q, orient='index')
print(df)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
DT 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
NN 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
POS 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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X 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
RBR 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
WP 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
JJR 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
WDT 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
RP 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
PDT 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
UH 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
NNPS 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
JJS 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
SYM 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
FW 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
WP$ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
LS 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Storing integers from text file to an array

I have a task to assign integers from a text file to an array in python.
I tried reading by lines and splitting, but none worked.
The task goes like this: we have an array
1 4 5 7 3 2 8 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 4 0 0 0 0 0
0 0 0 0 0 0 0 0 0 3 0 0 0 0
0 0 0 0 0 0 0 5 0 0 6 0 0 0
0 0 0 0 0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 2 9 0
0 0 0 0 0 0 0 0 0 0 10 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 11
0 0 0 0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 8
0 0 0 0 0 0 0 0 0 0 0 0 0 9
0 0 0 0 0 0 0 0 0 0 0 0 0 14
0 0 0 0 0 0 0 0 0 0 0 0 0 5
0 0 0 0 0 0 0 0 0 0 0 0 0 0
and this needs to be assigned to an array x in order to use it in further functions.
Do something like:
with open('my_raw_file.txt', 'r') as file:
all_file = file.read().strip() # Read and remove any extra new line
all_file_list = all_file.split('\n') # make a list of lines
final_data = [[int(each_int) for each_int in line.split()] for line in all_file_list] # make list of list and convert to int
print(final_data)
if you don't mind numpy arrays and pandas:
import pandas as pd
integers = pd.read_csv('test.txt', sep=" ", header=None)

Reordering rows based on number formed by combining columns in Python

I have a dataframe formed with pandas as seen below:
a b c d e f g h i j k l m n o
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 1 0 1 0 1 0 0 1 0
3 0 1 0 0 1 0 1 0 1 0 1 0 0 1 0
4 0 0 0 0 0 1 0 1 0 0 1 0 0 1 0
5 0 0 1 1 0 1 0 1 0 0 1 0 0 1 0
6 1 0 0 1 0 1 0 1 0 0 1 0 0 1 0
7 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
8 0 0 0 0 0 0 0 0 1 0 1 0 0 1 0
9 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0
10 0 0 0 1 0 1 0 1 0 0 1 0 0 1 0
11 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0
12 0 0 0 0 1 0 1 0 1 0 1 0 0 1 0
13 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0
14 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
15 0 0 0 0 1 0 1 0 1 0 1 0 0 1 0
16 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
I want to sort the rows so that they are ordered in descending order. The value of the row is the number formed by combining the columns. For example, row 1 is 000000000000000 and row 2 is 000000101010010. The final result should have Row 6 as the first row and row 1 as the last row. I've tried
dat.sort_values(by=['a'], ascending=False, axis=0)
but this only sorts by the first column. Is there another way I could reorder the rows?
Sort by all columns in their current order:
df.sort_values(by=df.columns.tolist(), ascending=False)
# a b c d e f g h i j k l m n o
#6 1 0 0 1 0 1 0 1 0 0 1 0 0 1 0
#3 0 1 0 0 1 0 1 0 1 0 1 0 0 1 0
#5 0 0 1 1 0 1 0 1 0 0 1 0 0 1 0
#10 0 0 0 1 0 1 0 1 0 0 1 0 0 1 0
#12 0 0 0 0 1 0 1 0 1 0 1 0 0 1 0
#15 0 0 0 0 1 0 1 0 1 0 1 0 0 1 0
#4 0 0 0 0 0 1 0 1 0 0 1 0 0 1 0
#2 0 0 0 0 0 0 1 0 1 0 1 0 0 1 0
#11 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0
#8 0 0 0 0 0 0 0 0 1 0 1 0 0 1 0
#13 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0
#9 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0
#16 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
#7 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
#14 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
#1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Using a key for sort
df.loc[df.astype(str).sum(1).sort_values(ascending=False).index]
Out[871]:
a b c d e f g h i j k l m n o
6 1 0 0 1 0 1 0 1 0 0 1 0 0 1 0
3 0 1 0 0 1 0 1 0 1 0 1 0 0 1 0
5 0 0 1 1 0 1 0 1 0 0 1 0 0 1 0
10 0 0 0 1 0 1 0 1 0 0 1 0 0 1 0
15 0 0 0 0 1 0 1 0 1 0 1 0 0 1 0
12 0 0 0 0 1 0 1 0 1 0 1 0 0 1 0
4 0 0 0 0 0 1 0 1 0 0 1 0 0 1 0
2 0 0 0 0 0 0 1 0 1 0 1 0 0 1 0
11 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0
8 0 0 0 0 0 0 0 0 1 0 1 0 0 1 0
13 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0
9 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0
16 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
7 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
14 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

CVXOPT GLPK returns wrong solution

I'm using python interface to GLPK.
I'm looking for a solution X that:
Minimizes c
G * X <= H
A * X = b
I'm using the following statement
glpk.ilp(c,G,h,A,b,I=set(range(25)))
Tha'ts my G matrix:
-1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0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 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0
0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 1
0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
That's my B matrix
21.0 21.0 21.0 21.0 21.0 21.0 21.0 21.0 21.0 21.0 21.0 21.0 21.0 21.0 21.0 21.0 21.0 21.0 21.0 21.0
Yet by executing the following:
sol=glpk.ilp(c,G,h,A,b,I=set(range(25)))
I get the following solution:
0 0 1 0 0 1 0 1 1 1 0 0 0 1 1 1 1 0 1 1 0 0 1 1 1
Which is clearly wrong.
Since
A * X != b
I don't have any lead to how to troubleshoot this. The documentation is scarce.
dI copied your arrays to text files (names should be self-explanatory) and ran the following code without problems.
Perhaps converting your arrays to proper cvxopt matrizes was the problem?
import numpy as np
import cvxopt
import cvxopt.glpk
c=cvxopt.matrix(np.loadtxt('c.txt'),tc='d')
A=cvxopt.matrix(np.loadtxt('A.txt'),tc='d')
b=cvxopt.matrix(np.loadtxt('b.txt'),tc='d')
G=cvxopt.matrix(np.loadtxt('G.txt'),tc='d')
h=cvxopt.matrix(np.loadtxt('h.txt'),tc='d')
sol=cvxopt.glpk.ilp(c,G,h,A,b,I=set(range(25)))
x=[i for i in sol[1]]
# x=[0 0 10 0 0 10 0 10 10 10 0 0 0 10 10 10 10 0 10 10 0 0 1 1 1]
eq=(np.dot(A,x)==b.T[0]).all()
# eq=True
Thus, the solution vector x is different from the one you reported.
In particular, the last line checks for equality A*x=b and evaluates to true.
Hope that helps!

Categories