Wednesday, August 2, 2017

Python: selection a column based on the values in another column and saving it into CSV file

Example:
index, column1_name,column2_name
1, asdb, 2
2, asdfsf, 1
3, asdasdfasd, 1
4, dgfg, 2


Results will be
asdb
dgfg




import numpy as np
import pandas as pd
#reading the data
dataframe = pd.read_csv('labeled_data.csv')

for i, col in enumerate(dataframe.columns):
    print(i, col)


newData=dataframe .loc[dataframe ['column2_name'] == 2]
newData2 = newData['column1_name']
newData2.to_csv("foo2.csv",index=False )

No comments:

Post a Comment