Posts

Showing posts with the label pandas

Pandas - Check adjacent column for value

Pandas - Check adjacent column for value I have a df that tracks the status of an issue. From 'Open', 'In Progress' to 'Closed' like such: T1 T2 T3 T4 T5 1 Open In Progress Closed 2 In Progress Closed 3 Open In Progress Open Closed 4 Open In Progress Closed Open Closed 5 Open In Progress Closed Basically I want to find all issues that get reopened. This can be noted by any row that has a Closed value that then has a followup transition. For example, index  4  has a closed value in  T3  but then  T4  contains something to indicate it has been reopened. Closed 4 T3 T4 The output would be: T1 T2 T3 T4 T5       Reopened 1 Open In Progress Closed                       0 2 In Progress Closed                                   0   3 Open In Progress Open Closed               0 4 Open In Progress Cl...

python count /sum records accross row with multiple condition [on hold]

python count /sum records accross row with multiple condition [on hold] I current have a table which A,B,C represents product values while AA,BB,CC represents product type A B C AA BB CC 10 20 0 1 2 0 5 0 15 1 2 3 i also have 2 arrays which are: product =['A','B','C'] Product Value = ['AA','BB','CC'] My question is 1) To count/sum corresponding product type if its product value >0 product value >0 Product value Product Type(PT) count/sum same product if its value >0 ------------- ---------------- ---------------------------------------------------- A B C AA BB CC product_1 product_2 product_1_sum product_2_sum 10 20 0 1 1 0 2 0 30 0 5 0 15 0 2 2 0 2 0 15 for first record, product A and B have value > 0 and A and B are...

Searching a Python Dictionary with multiple values

Image
Searching a Python Dictionary with multiple values I have data in the following csv file, available here: http://s000.tinyupload.com/index.php?file_id=87473936848618674050 Screenshot of the CSV: I've written the following code to import the CSV file into Python as a Pandas Dataframe, and then the code after that creates a Dict. The dictionary has to have name and region as the keys, and the Windows and Linux prices as the dictionary values. #Import libraries and CSV file into dataframe, renaming columns, printing head import pandas as pd df = pd.read_csv('file.csv') col_names = ['Name','Region','API', 'Memory','vCPU', 'Storage', 'Linux', 'Windows' ] df.columns = col_names #Creating Dict dict = {} for i in df.index: key = (df.at[i, 'Name'] , df.at[i, 'Region']) #Rename columns accordingly value = (df.at[i, 'vCPU'], df.at[i, 'Memory'], df.at[i, 'Storage'], df.at[i, ...

How to extract the first 2 digits of all numbers in a column of a dataframe?

How to extract the first 2 digits of all numbers in a column of a dataframe? I am completely new at Python (this is my first assignment) and I am trying to take the first two digits of the D-column of the following dataframe and put those two digits in a new column F: import pandas as pd import numpy as np df1 = pd.DataFrame({'A' : [1, 1, 1, 4, 5, 3, 3, 4, 1, 4], 'B' : [8, 4, 3, 1, 1, 6, 4, 6, 9, 8], 'C' : [69,82,8,25,56,79,98,68,49,82], 'D' : [1663, 8818, 9232, 9643, 4900, 8568, 4975, 8938, 7513, 1515], 'E' : ['Married','Single','Single','Divorced','Widow(er)','Single','Married','Divorced','Married','Widow(er)']}) I found several possible solutions here on Stack Overflow, and tried to apply them but none of them is working for me. Either I get some error message (different depending on wh...

Can't convert dataframe from float64 to string to compare values using ISIN

Can't convert dataframe from float64 to string to compare values using ISIN I am reading in one column of data as a dataframe from two different Excel worksheets (df1, df2), so that I can use the ISIN command to determine what values in df1 are missing from df2. The values are for example like 4533.01, and 3578.05. They are actually string values but the dataframe info reports that they are stored as float64 values. When I performed an ISIN command, I got FALSE for all 1000+ entries. So I figured that I would change the dataframe to a string using df1['cola'] = df1['cola'].astype(str) but the dataframe still remains as a float64. I looked at this post; pandas - change df.index from float64 to unicode or string. but it did not help me too much. Why do i need to do convert the dataframe to a string or any other method, so that I can properly use ISIN on the two dataframes? Each dataframe has only 1 column of data. Data sample from df1 In [51]: df1 Out[51]: A...

Would like to read in Two columns of dates but only get One

Would like to read in Two columns of dates but only get One I have a text.csv file with 6 columns. I want 2 columns read in as dates for later differences. However, I only get ONE column coming back as a datetime . Any ideas? Also, I have several empty dates that return nan NOT 0(zeros) as in na_values = 0 ?? import pandas as pd CSV = 'text.csv' df = pd.read_csv(CSV, skiprows = 0, na_values = 0, parse_dates = ['Date of Sign Up', 'Birth Date'], usecols = ['Date of Sign Up', 'A', 'B', 'C', 'D', 'Birth Date']) df.info() # Check info for column types and nan... RangeIndex: 969 entries, 0 to 968 Data columns (total 6 columns): Date of Sign Up 969 non-null datetime64[ns] A 969 non-null object B 969 non-null object C 969 non-null object D 969 non-null object Birth Date 969 non-null obj...

Pandas apply on custom function results in segmetnation fault

Pandas apply on custom function results in segmetnation fault I am applying a custom method on a dataframe using the apply method. When a dataframe with more than 2 rows (tuples) are passed, it results in the kernel being terminated (dead) in the jupyter notebook. When running the same on terminal, it results in a segmentation Fault . The method works for an individual row or for 2 rows, but not more than that. Both the calls below works with the custom function myTrial . myTrial myTrial(pd.ix[3,:]) newPD2 = pd.head(2).apply(myTrial, axis=1) But this results in the following error. newPD2 = pd.head(3).apply(myTrial, axis=1) The kernel appears to have died. It will restart automatically. The method myTrial uses alignment function pairwise2.align.globalmx from BioPython and other inbuilt python functions. I am providing the function below: myTrial pairwise2.align.globalmx BioPython I am having a dataframe with 10,000 rows and 8 columns. I am suing a server with 256 GB RAM. The functio...

Copy pandas DataFrame row to multiple other rows

Copy pandas DataFrame row to multiple other rows Simple and practical question, yet I can't find a solution. The questions I took a look were the following: Modifying a subset of rows in a pandas dataframe Changing certain values in multiple columns of a pandas DataFrame at once Fastest way to copy columns from one DataFrame to another using pandas? Selecting with complex criteria from pandas.DataFrame The key difference between those and mine is that I need not to insert a single value, but a row. My problem is, I pick up a row of a dataframe, say df1 . Thus I have a series. df1 Now I have this other dataframe, df2 , that I have selected multiple rows according to a criteria, and I want to replicate that series to all those row. df2 df1: Index/Col A B C 1 0 0 0 2 0 0 0 3 1 2 3 4 0 0 0 df2: Index/Col A B C 1 0 0 0 2 0 0 0 3 0 0 0 4 0 0 0 What I want to accomplish is ins...