Atom Store
Atom Store

Reputation: 1006

How to ignore empty columns in a dataframe?(Pandas)

I want to ignore empty columns in a dataframe.

For Example:

sample.csv

Id  Name  Address    Contact   Item   Rate Qty  Price
1   Mark  California 98429102  Shirt  57    2    8
2   Andre Michigan   92010211

I have tried:

import pandas as pd
df = pd.read_csv('sample.csv')
df = df.fillna('')

df.to_csv('sample.txt',sep='*',index=False, header=False)

The sample.txt looks like

1*Mark*California*98429102*Shirt*57*2*8
2*Andre*Michigan*92010211****

I want to remove the empty columns here. The sample.txt should look like this:

1*Mark*California*98429102*Shirt*57*2*8
2*Andre*Michigan*92010211

Upvotes: 2

Views: 1822

Answers (2)

keepAlive
keepAlive

Reputation: 6665

What about

sep = '*'
(
    df
    .applymap(str)
    .apply(
        # Removes all empty fields
        # axis=1, func=lambda s: sep.join(el for el in s if el)
        # Removes trailing fields
        axis=1, func=lambda s: sep.join(s).strip('*')
    )
    .to_csv('sample.txt', index=False, header=False)
)

Upvotes: 1

Rob Raymond
Rob Raymond

Reputation: 31206

Just use a memory buffer and strip()

import io
df = pd.read_csv(io.StringIO("""1*Mark*California*98429102*Shirt*57*2*8
2*Andre*Michigan*92010211****"""), sep="*", header=None)

with open("sample.csv", "w") as f: 
    f.write("\n".join([l.strip("*") for l in df.to_csv(sep="*",header=None, index=None).split("\n")]))

with open("sample.csv") as f: print(f.read())

output

1*Mark*California*98429102*Shirt*57.0*2.0*8.0
2*Andre*Michigan*92010211

Upvotes: 2

Related Questions