import numpy as np
shape = (4,4)
np.core.defchararray.add('x_',(((np.arange(shape[0])+1)*10).reshape(-1,1)+1 + np.arange(shape[1])).astype(np.str))
array([['x_11', 'x_12', 'x_13', 'x_14'],
['x_21', 'x_22', 'x_23', 'x_24'],
['x_31', 'x_32', 'x_33', 'x_34'],
['x_41', 'x_42', 'x_43', 'x_44']],
dtype='<U13')
import datetime
import json
in_data = {}
in_data['2018']={'11': ['3', '4', '5', '10', '11', '17', '18', '24', '25'],
'10': ['6', '7', '13', '14', '20', '21', '27', '28'],
'12': ['1', '2', '8', '9', '15', '16', '22', '23', '29', '30', '31*'],
'1': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '13', '14', '20', '21', '27', '28'],
'3': ['3', '4', '7*', '8', '10', '11', '17', '18', '24', '25', '31'],
'2': ['3', '4', '10', '11', '17', '18', '22*', '23', '24', '25'],
'5': ['1', '5', '6', '8*', '9', '12', '13', '19', '20', '26', '27'],
'4': ['1', '7', '8', '14', '15', '21', '22', '28', '29', '30*'],
'7': ['1', '7', '8', '14', '15', '21', '22', '28', '29'],
'6': ['2', '3', '9', '10', '11*', '12', '16', '17', '23', '24', '30'],
'9': ['1', '2', '8', '9', '15', '16', '22', '23', '29', '30'],
'8': ['4', '5', '11', '12', '18', '19', '25', '26']}
print(json.dumps(dict([[year,
dict([[month,
dict([[day,
{'isWorking':2}] for day in in_data[year][month]])
] for month in in_data[year]]
)] for x in in_data]
), indent=4, sort_keys=4
))
{
"2018": {
"1": {
"1": {
"isWorking": 2
},
"10": {
"isWorking": 2
},
"13": {
"isWorking": 2
},
"14": {
"isWorking": 2
},
"2": {
"isWorking": 2
},
"20": {
"isWorking": 2
},
"21": {
"isWorking": 2
},
"27": {
"isWorking": 2
},
"28": {
"isWorking": 2
},
"3": {
"isWorking": 2
},
"4": {
"isWorking": 2
},
"5": {
"isWorking": 2
},
"6": {
"isWorking": 2
},
"7": {
"isWorking": 2
},
"8": {
"isWorking": 2
},
"9": {
"isWorking": 2
}
},
"10": {
"13": {
"isWorking": 2
},
"14": {
"isWorking": 2
},
"20": {
"isWorking": 2
},
"21": {
"isWorking": 2
},
"27": {
"isWorking": 2
},
"28": {
"isWorking": 2
},
"6": {
"isWorking": 2
},
"7": {
"isWorking": 2
}
},
"11": {
"10": {
"isWorking": 2
},
"11": {
"isWorking": 2
},
"17": {
"isWorking": 2
},
"18": {
"isWorking": 2
},
"24": {
"isWorking": 2
},
"25": {
"isWorking": 2
},
"3": {
"isWorking": 2
},
"4": {
"isWorking": 2
},
"5": {
"isWorking": 2
}
},
"12": {
"1": {
"isWorking": 2
},
"15": {
"isWorking": 2
},
"16": {
"isWorking": 2
},
"2": {
"isWorking": 2
},
"22": {
"isWorking": 2
},
"23": {
"isWorking": 2
},
"29": {
"isWorking": 2
},
"30": {
"isWorking": 2
},
"31*": {
"isWorking": 2
},
"8": {
"isWorking": 2
},
"9": {
"isWorking": 2
}
},
"2": {
"10": {
"isWorking": 2
},
"11": {
"isWorking": 2
},
"17": {
"isWorking": 2
},
"18": {
"isWorking": 2
},
"22*": {
"isWorking": 2
},
"23": {
"isWorking": 2
},
"24": {
"isWorking": 2
},
"25": {
"isWorking": 2
},
"3": {
"isWorking": 2
},
"4": {
"isWorking": 2
}
},
"3": {
"10": {
"isWorking": 2
},
"11": {
"isWorking": 2
},
"17": {
"isWorking": 2
},
"18": {
"isWorking": 2
},
"24": {
"isWorking": 2
},
"25": {
"isWorking": 2
},
"3": {
"isWorking": 2
},
"31": {
"isWorking": 2
},
"4": {
"isWorking": 2
},
"7*": {
"isWorking": 2
},
"8": {
"isWorking": 2
}
},
"4": {
"1": {
"isWorking": 2
},
"14": {
"isWorking": 2
},
"15": {
"isWorking": 2
},
"21": {
"isWorking": 2
},
"22": {
"isWorking": 2
},
"28": {
"isWorking": 2
},
"29": {
"isWorking": 2
},
"30*": {
"isWorking": 2
},
"7": {
"isWorking": 2
},
"8": {
"isWorking": 2
}
},
"5": {
"1": {
"isWorking": 2
},
"12": {
"isWorking": 2
},
"13": {
"isWorking": 2
},
"19": {
"isWorking": 2
},
"20": {
"isWorking": 2
},
"26": {
"isWorking": 2
},
"27": {
"isWorking": 2
},
"5": {
"isWorking": 2
},
"6": {
"isWorking": 2
},
"8*": {
"isWorking": 2
},
"9": {
"isWorking": 2
}
},
"6": {
"10": {
"isWorking": 2
},
"11*": {
"isWorking": 2
},
"12": {
"isWorking": 2
},
"16": {
"isWorking": 2
},
"17": {
"isWorking": 2
},
"2": {
"isWorking": 2
},
"23": {
"isWorking": 2
},
"24": {
"isWorking": 2
},
"3": {
"isWorking": 2
},
"30": {
"isWorking": 2
},
"9": {
"isWorking": 2
}
},
"7": {
"1": {
"isWorking": 2
},
"14": {
"isWorking": 2
},
"15": {
"isWorking": 2
},
"21": {
"isWorking": 2
},
"22": {
"isWorking": 2
},
"28": {
"isWorking": 2
},
"29": {
"isWorking": 2
},
"7": {
"isWorking": 2
},
"8": {
"isWorking": 2
}
},
"8": {
"11": {
"isWorking": 2
},
"12": {
"isWorking": 2
},
"18": {
"isWorking": 2
},
"19": {
"isWorking": 2
},
"25": {
"isWorking": 2
},
"26": {
"isWorking": 2
},
"4": {
"isWorking": 2
},
"5": {
"isWorking": 2
}
},
"9": {
"1": {
"isWorking": 2
},
"15": {
"isWorking": 2
},
"16": {
"isWorking": 2
},
"2": {
"isWorking": 2
},
"22": {
"isWorking": 2
},
"23": {
"isWorking": 2
},
"29": {
"isWorking": 2
},
"30": {
"isWorking": 2
},
"8": {
"isWorking": 2
},
"9": {
"isWorking": 2
}
}
}
}
import numpy as np
import pandas as pd
print('pandas version: ' + pd.__version__)
print()
print('Source dataframe:')
X = pd.DataFrame([['John', 10],
['Mike', None],
['Alice', 20],
['Eve', None]], columns=['Name', 'Salary'])
print(X)
X['Salary'].fillna(0.0, inplace=True)
print()
print('Target dataframe:')
print(X)
pandas version: 0.18.1
Source dataframe:
Name Salary
0 John 10.0
1 Mike NaN
2 Alice 20.0
3 Eve NaN
Target dataframe:
Name Salary
0 John 10.0
1 Mike 0.0
2 Alice 20.0
3 Eve 0.0
import numpy as np
import pandas as pd
print('pandas version: ' + pd.__version__)
df1 = pd.DataFrame([[1,'M'],
[2 ,'M',],
[3,'F'],
[4,'F']], columns=['customer_id', 'gender'])
print('df1:')
print(df1)
df2 = pd.DataFrame([[1,100,'yellow'],
[2 ,150,'black'],
[3,10, 'black'],
[4,700,'red'],
[5,200,'green'],
[6,170,'white']], columns=['customer_id', 'feature1', 'feature2'])
print('df2:')
print(df2)
train_df=df2[df2['customer_id'].isin(df1['customer_id'])]
print('train_df:')
print(train_df)
test_df=df2[~df2['customer_id'].isin(df1['customer_id'])]
print('test_df:')
print(test_df)
pandas version: 0.18.1
df1:
customer_id gender
0 1 M
1 2 M
2 3 F
3 4 F
df2:
customer_id feature1 feature2
0 1 100 yellow
1 2 150 black
2 3 10 black
3 4 700 red
4 5 200 green
5 6 170 white
train_df:
customer_id feature1 feature2
0 1 100 yellow
1 2 150 black
2 3 10 black
3 4 700 red
test_df:
customer_id feature1 feature2
4 5 200 green
5 6 170 white