In Python I want to break up one data-frame and write -each "Key","Column" pair to a CSV file. The rows also sometimes contain more than one value per column.
The data-frame looks as follows (the number of columns will not always be the same)
col1 col2 key col3 a b c d a1 b1 c1 d1,d4 a2 b2,b4 c2 d2 a3 b3 c3 d3
What I need is 3 CSV files, looking as follows:
col.csv key col1 c a c1 a1 c2 a2 c3 a3 col2.csv key col2 c b c1 b1 c2 b2 c2 b4 c3 b3 col3.csv key col3 c d c1 d1 c1 d4 c2 d2 c3 d3
What I've done is write out each column to CSV, with the column name as the CSV, I however cannot get the "key" column to the CSV's.
z = df[df_obj.columns] for c in z.columns: df[c].to_csv(cwd + '/split/' + c + '.csv', index=False)
What I get is:
col1.csv col1 a a1 a2 a3 col2.csv col2 b b1 b2,b4 b3 col3.csv col3 d d1,d4 d2 d3
Would you mind trying the following. The idea is just to extract the keys column out as a separate series first, which you can then use when you iterate over the columns.
import os import pandas as pd >>>df col1 col2 key col3 a b c d a1 b1 c1 d1,d4 a2 b2,b4 c2 d2 a3 b3 c3 d3 # Create a key series keys = [r for r in df.key] # Exclude the keys column from the dataframe df = df.drop('key', axis=1) for c in df.columns: ser1 = df[c].str.split(',') data = pd.DataFrame([(keys[i], j) for i in range(len(keys)) for j in ser1[i]]) data.to_csv(os.path.join(os.getcwd(),c,'.csv'), index=False)
- 'Entry' object has no attribute 'set'
- How do I access text between tags with xml.etree.ElementTree
- How to groupby column name and join
- How to select context words/characters surrounding an <a> tag using BeautifulSoup?
- How to use operator.itemgetter to check a condition in a filter function instead of a lambda?
- How do I get the file / key size in boto S3?
- What does if __name__ == “__main__”: do?
- How to get String Before Last occurrence of substring?
- Is it possible to call the constructor of a class that inherits Enum without arguments?
- How does this regularization code affect loss?