简体   繁体   中英

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

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

What about

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM