How to Save a Data Frame in a Txt or Excel File Separated by Columns

How to save each column of a data frame into separate sheets in one excel file

You can do this:

data = {'Name': ['Tom', 'Joseph', 'Krish', 'John'], 'Age': [20, 21, 19, 18]}  
df = pd.DataFrame(data)

with pd.ExcelWriter('output.xlsx') as writer:
for col in df.columns:
df[col].to_excel(writer, sheet_name=col, index=False)

df.columns is a list of column names

sheet_name = column name

Python, Pandas : write content of DataFrame into text File

You can just use np.savetxt and access the np attribute .values:

np.savetxt(r'c:\data\np.txt', df.values, fmt='%d')

yields:

18 55 1 70
18 55 2 67
18 57 2 75
18 58 1 35
19 54 2 70

or to_csv:

df.to_csv(r'c:\data\pandas.txt', header=None, index=None, sep=' ', mode='a')

Note for np.savetxt you'd have to pass a filehandle that has been created with append mode.

How can I seperate my dataframe according to a column values and export it to excel file?

You can take advantage of groupby.

group = df.groupby('client_id')

for key, df in group:
df.to_excel(f'{key}.xlsx')

data frame to file.txt python

This is an almost exact duplicate of the following:

Python, Pandas : write content of DataFrame into text File

I report again here the answer from the cited SO question with some very small modifications to fit this case.

You can use two methods.

np.savetxt(), in which case you should have something like the following:

np.savetxt('xgboost.txt', a.values, fmt='%d', delimiter="\t", header="X\tY\tZ\tValue")  

assuming a is the dataframe. Of course you can change the delimiter you want (tab, comma, space,etc.).

The other option, as mentioned in the answer I attached and in the answer here from @MYGz, is to use the to_csv method, i.e.:

a.to_csv('xgboost.txt', header=True, index=False, sep='\t', mode='a')

R: How to aggregate several data frames into a txt file via for loop?

You writing out txt_file_data_frame when you should be writing out result.

Without changing your code just replace the second for loop, which you don't need at all with:

## creating a data frame to put our data inside it:   
txt_file_data_frame <- data.frame(year_date, month_date, day_date, Alberta_column)

result[[i]] <- txt_file_data_frame
} # end of for loop

Then to write out your file, bind together your results and write to disk

txt_out <- do.call(rbind, result)

## write the txt file:
write.table(txt_out, file = "myTXT.txt", row.names = FALSE, dec = ".", sep = "\t", quote = FALSE)

You can also drop counter as it is not needed.

Save data frame containing a list column as a delimited txt file

I’m not aware of a builtin way of saving list columns. In your particular case, you could convert the list column to a character column by pasting the values:

productSales$Product = unlist(lapply(productSales$Product, paste, collapse = ', '))

Afterwards, you can use write.table (or a more modern equivalent such as readr::write_csv).

Python: Print rows into individual text files with columns separated by linebreak

You could do it like this:

vals = [(1, 0, 'cat', 34, 'k'), (2, 1, 'dog', 30, 'g')]
df = pd.DataFrame(vals, columns=['Id', 'col1', 'col2', 'col3', 'col4'])

subset = df.loc[df.col1 == 1] #subset for values equal to one in the col1

#loop through
for i, r in subset.iterrows():
for_write = '{} \n {}'.format(r.col2, r.col3)
print(for_write)
with open('{}.txt'.format(r.Id), 'w') as text_file:
text_file.write(for_write)


Related Topics



Leave a reply



Submit