Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
372 views
in Technique[技术] by (71.8m points)

python - Panda Dataframe to.string with a decimal point (,)

could you help me to solve the following task, please?

My dataframe has the following content:

1 BEN 2 BIIB 3 BMY 4 COG 5 GPS 6 HAL 7 IPG 8 LLY 9 LOW 10 LUV 11 MRK 12 PSX 13 RMD 14 ROP 15 STT 16 UAA

I would like to save the dataframe df as a txt file. But I need the txt output in the following format (separated by commas): BEN,BIIB,BMY,COG,GPS,HAL,IPG,LLY,LOW,LUV,MRK,PSX,RMD,ROP,STT,UAA

I tried to use:

dfnew = df.to_string(decimal=str)

But that is not the solution.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If df is Series join values by , and write to file:

with open("file.txt", "w") as f:
    f.write(','.join(df))
        

If values are in column:

with open("file.txt", "w") as f:
    f.write(','.join(df['column']))
        

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...