Count the number of null values using pandas framework [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Write python code to find the total number of null values on the excel file without using isnull function ( you should use loop statement)

Using for statement in dataframes is not a good practice. If you're doing only for challenge, this could help:
data = pd.read_excel('data.xlsx')
data.fillna(False, inplace=True)
amount_nan = 0
for column in data:
for value in data[column]:
if not value:
amount_nan = amount_nan + 1
print(amount_nan)

Related

How to split a list into 2 digit numbers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
The community is reviewing whether to reopen this question as of 12 months ago.
Improve this question
I have this:
a = ['a','b','c','d','e','f']
And I want to make it into this:
a = ['ab','cd','ef']
I tried this:
for i in range(len(a)):
if round(i/2) > len(a):
break
c = (a[(i*2)-1])+(a[i*2])
c_list.append(c)
But it didn't work, and I felt it wasn't the best way to approach it.
def pair_up(a):
return [a[i]+a[i+1] for i in range(0,len(a),2)]
print(pair_up(a))

Using Regex to drop period from many column names [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'd like to drop a '.' from a column name using regex, and want the code to be applied to many column names that end in '.', so that each pair of like-named columns can be merged into one.
For example, the column names 'Fund' and 'Fund.' are different and have different values, but should become just 'Fund'.
What would be the best regex to use for this?
Try this:
df = pd.DataFrame([1], columns=['Fund.'])
df.columns = df.columns.str.replace('.','')
Output:
print(df.columns)
Index(['Fund'], dtype='object')

In python how to store the values of loop in a python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I tried to append these values in the list but it appends only else part.
I want to store these values i printed in a list. How can i achieve this??
for i in d["Cabin new"]:
if i=="nan":
print ("na")
else:
print (i[1:])
Append in both places:
res = []
for i in d["Cabin new"]:
if i=="nan":
res.append('na')
else:
res.append(i[1:])

How to do pandas column selection while getting code completion? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
When working with pandas, I often use name based column indexing. E.g:
df = pd.DataFrame({"abc":[1,2], "bde":[3,4], "mde":[3,4]})
df[["mde","bde"]]
As I have longer column names it because easy for me to create a typo in the column names since they are strings and no code completion. It'd be great if I could do something like:
df.SelectColumnsByObjectAttributeNotString([df.mde, df.bde])
IIUC, you can use name attribute.
df = pd.DataFrame({"a":[1,2], "b":[3,4]})
columns = [df.a.name, df.b.name]
columns
['a', 'b']
I think you may be looking for:
df.columns.values.tolist()

Data extract from text file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Please help.
I have data in text file with 2 long columns tab spaced
I have to extract first and second columns separately
Can it be done with Python ?
If you need the columns as two separate lists:
first_column = []
second_column = []
data_file = open('your_file.txt')
for line in data_file.readlines():
first_column.append(line.split('\t')[0])
second_column.append(line.split('\t')[1])
data_file.close()

Categories

Resources