for loop to create a matrix in python

2024/10/14 4:27:31

I am trying to study the probability of having a zero value in my data and I have developed a code that outputs the value of a column of data when the other is zero which is what I need. But having to do that for each column vs all other 28 of my 577by29 dataframe is difficult so I decided to create a for loop that does that for me where I have this:

import numpy as np
import pandas as pd
allchan = pd.read_csv('allchan.csv',delimiter = ' ')
allchanarray = np.array(allchan)
dfallchan = pd.DataFrame(allchanarray,range(1,578),dtype=float)
y = pd.DataFrame()
x = pd.DataFrame()
for n in range(0,29):x[n] = dfallchan[(dfallchan[0]>0) & (dfallchan[n]==0)][0]y[n] = x[n].count()
x.to_excel('n.xlsx', index=False, sheet_name='ValForOtherZero')
y.to_excel('v.xlsx', index=False, sheet_name='CountOfZeroVlas')

The problem that is that the loop for some reason goes properly through the lines:

 x[n] = dfallchan[(dfallchan[0]>0) & (dfallchan[n]==0)][0]y[n] = x[n].count()

but it repeats the value of n=6 for the second condition:

(dfallchan[n]==0)

the output of the code should return different values of the first channel as the zeros are randomly distributed in my input file, but my output is correct for the data until the the 6th column -as my columns(0-5) should be empty- where it repeats the output for all other columns! output: output 1

you can see that the code loops correctly as the output data frame has n=29 columns but not for the condition specified above.

Please help, Thanks!

Answer

Finally Got it!

This code does exactly what I want!

# In[9]:import numpy as np
import pandas as pd# In[10]:allchan = pd.read_csv('allchan.csv',delimiter = ' ')# In[11]:allchanarray = np.array(allchan)# In[12]:dfallchan = pd.DataFrame(allchanarray,range(1,578),dtype=float)# In[13]:v = pd.DataFrame(columns=range(0,29))
y = pd.DataFrame()
k = pd.DataFrame(columns=range(0,29))# In[14]:for n in range(0,29):x = dfallchan[(dfallchan[0]>0) & (dfallchan[n]==0)][0]y = y.append(x)v = y.transpose()k = v.count()# In[15]:v.columns=range(0,29)
k = k.values.reshape(1,29)# In[16]:v.to_excel("Chan1-OthersZeroVals.xlsx", index=False)
pd.DataFrame(k).to_excel("Chan1-OtherZeroCount.xlsx", index=False)
https://en.xdnf.cn/q/117997.html

Related Q&A

How to convert List of JSON frames to JSON frame

I want to convert List of JSON object ot Single JSON frameHere is my codefor i in user1:name=i.namepassword=i.passwordid1=i.iduser = { "name" : name,"password" : password,"id&q…

Python 2.7 The packaging package is required; normally this is bundled with this package

I expect this has to do with the cryptography module, but Im not sure.Traceback (most recent call last):File "<string>", line 11, in <module>File "c:\python27\lib\site-packag…

Couple the data in all possible combinations

I have data in column in two columns like thisId Value 1 a 2 f 1 c 1 h 2 aand Id like couple the data of the Value column in all possible combinations based on the same Id such as(a,c) (a,h)…

Python - Find date from string

Would anyone know a regex string or another method of obtaining the date and time from this string into variables? The position of the string could change, so line and char no would not work. This is …

Get 1st column values on .csv file on python

i am newbie at python programming, i have a .csv file containing mac address and signal strength data from an AP consider my csv data is:i want to get just mac address values which is the 1st row, ref…

how to click mouse over sub menu in selenium?

I want to click invisible htmls sub menu click.*invisible html source<ul class="options"> <li class="_ranking-attr-filter-container _sub-menu-target"> <span>Hide w…

SSL: CERTIFICATE_VERIFY_FAILED certificate verify failed (_ssl.c.661)

Im trying to install nltk on my Mac, but I keep getting this error message after following these instructions: Install NLTK: sudo pip install -U nltk Install Numpy (optional): sudo pip install -U numpy…

Real-time reading of terminal output from server

Im trying to process images from my camera on my server and get the information after processing on my local machine in real-time. I can get necessary information as terminal outputs on my server, but …

Transform map to mapPartition using pyspark

I am trying to load a tensorflow model from disk and predicting the values.Codedef get_value(row):print("**********************************************")graph = tf.Graph()rowkey = row[0]check…

Module google_auth_httplib2 not found after pip installing google-cloud How can I fix it?

I used pip to install cloud-storage, like this:$ pip install --upgrade google-cloudWhen I started my application, I got an error that said no module named google_auth_httplib2 was found. I used pip lis…