Reputation: 1006
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
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
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())
1*Mark*California*98429102*Shirt*57.0*2.0*8.0
2*Andre*Michigan*92010211
Upvotes: 2