Creating a dataframe with multi-index columns from a dictionary - python-3.x

Suppose I have the following:
a data frame with multi-index columns
a dictionary that's been created from the data frame with multi-index columns in order to efficiently manipulate it.
df = pd.DataFrame([
[100,90,80,70,36,45],
[101,78,65,88,55,78],
[92,77,42,79,43,32],
[103,98,76,54,45,65]],
index = pd.date_range(start='2022-01-01', periods=4)
)
df.columns = pd.MultiIndex.from_tuples(
(("mkf", "Open"),
("mkf", "Close"),
("tdf", "Open"),
("tdf","Close"),
("ghi","Open"),
("ghi", "Close"))
)
df
mkf tdf ghi
Open Close Open Close Open Close
2022-01-01 100 90 80 70 36 45
2022-01-02 101 78 65 88 55 78
2022-01-03 92 77 42 79 43 32
2022-01-04 103 98 76 54 45 65
df_dict = {c:df[c].assign(r=np.log(df[(c, 'Close')]).diff()) for c in df.columns.levels[0]}
df_dict
{'ghi': Open Close r
2022-01-01 36 45 NaN
2022-01-02 55 78 0.550046
2022-01-03 43 32 -0.890973
2022-01-04 45 65 0.708651,
'mkf': Open Close r
2022-01-01 100 90 NaN
2022-01-02 101 78 -0.143101
2022-01-03 92 77 -0.012903
2022-01-04 103 98 0.241162,
'tdf': Open Close r
2022-01-01 80 70 NaN
2022-01-02 65 88 0.228842
2022-01-03 42 79 -0.107889
2022-01-04 76 54 -0.380464}
What is the best way to transform the dictionary back to a data frame in its original form (i.e. with multi-index columns)?
mkf tdf ghi
Open Close r Open Close r Open Close r
2022-01-01 100 90 NaN 80 70 NaN 36 45 NaN
2022-01-02 101 78 0.55 65 88 -0.14 55 78 0.23
2022-01-03 92 77 -0.89 42 79 -0.12 43 32 -0.10
2022-01-04 103 98 0.71 76 54 0.24 45 65 -0.38

pd.concat([df_dict[c] for c in df_dict.keys()], axis = 1,
keys = df_dict.keys())
ghi mkf tdf
Open Close r Open Close r Open Close r
2022-01-01 36 45 NaN 100 90 NaN 80 70 NaN
2022-01-02 55 78 0.550046 101 78 -0.143101 65 88 0.228842
2022-01-03 43 32 -0.890973 92 77 -0.012903 42 79 -0.107889
2022-01-04 45 65 0.708651 103 98 0.241162 76 54 -0.380464

Related

Python-3 - get values from list by frequency and then by the values with equal counts in descending order

I have list of integers, from which I would first like to get unique numbers, first ordered by their occurrences and then the numbers with equal counts should be ordered in descending order.
example 1:
input1 = [1,2,2,1,6,2,1,7]
expected output = [2,1,7,6]
explanation: both 2 and 1 appear thrice while 6 and 7 appear once. so, the numbers occurring thrice will be placed first and in descending order; and same for the set that appears once.
another example case:
input_2 = list(map(int, '40 29 2 44 30 79 46 85 118 66 113 52 55 63 48 99 123 51 110 66 40 115 107 46 6 114 36 99 13 108 85 39 14 121 42 37 56 11 104 28 24 123 63 51 118 52 120 28 64 43 44 86 42 71 101 78 93 1 6 14 42 33 88 107 35 70 74 30 54 76 27 91 115 71 63 103 94 109 39 4 16 108 97 83 29 57 86 121 53 94 28 7 5 31 123 21 2 17 112 104 75 124 88 30 108 14 65 118 28 81 80 14 14 107 21 60 47 97 50 53 19 112 43 46'.split()))
output_2 = list(map(int, '14 28 123 118 108 107 63 46 42 30 121 115 112 104 99 97 94 88 86 85 71 66 53 52 51 44 43 40 39 29 21 6 2 124 120 114 113 110 109 103 101 93 91 83 81 80 79 78 76 75 74 70 65 64 60 57 56 55 54 50 48 47 37 36 35 33 31 27 24 19 17 16 13 11 7 5 4 1'.split()))
This was from a coding test I took. This must be solved without using functions from imports like collections, itertools etc,. and using functions already available in python's namespace like dict, sorted is allowed. How do I do this as efficiently as possible?
def sort_sort(input1):
a = {i:input1.count(i) for i in set(input1)}
b ={i:[] for i in set(a.values())}
for k,v in a.items():
b[v].append(k)
for v in b.values():
v.sort(reverse=True)
output=[]
quays =list(b.keys())
quays.sort(reverse=True)
for q in quays:
output +=b[q]
print(output)

How does one create a dictionary from a data-frame with a multi-index column?

How can I create a dictionary from the following df? Trying to use the to_dict method but not having much luck.
df = pd.DataFrame([[100,90,80,70,36,45], [101,78,65,88,55,78], [92,77,42,79,43,32], [103,98,76,54,45,65]], index = pd.date_range(start='2022-01-01' ,periods=4))
df.columns = pd.MultiIndex.from_tuples((("mkf", "Open"), ("mkf", "Close"), ("tdf", "Open"), ("tdf","Close"), ("ghi","Open"), ("ghi", "Close")))
df
mkf tdf ghi
Open Close Open Close Open Close
2022-01-01 100 90 80 70 36 45
2022-01-02 101 78 65 88 55 78
2022-01-03 92 77 42 79 43 32
2022-01-04 103 98 76 54 45 65
The desired outcome is a dictionary where the keys are mkf, tdf, ghi and the values would be the contents of the sub data frames:
{'mkf' :
Open Close
2022-01-01 100 90
2022-01-02 101 78
2022-01-03 92 77
2022-01-04 103 98,
'tdf' : Open Close
2022-01-01 80 70
2022-01-02 65 88
2022-01-03 42 79
2022-01-04 76 54,
'ghi':
Open Close
2022-01-01 36 45
2022-01-02 55 78
2022-01-03 43 32
2022-01-04 45 65
}
Are you looking for something like this?
dct = {c: df[c] for c in df.columns.get_level_values(0).unique()}
Output:
{'mkf': Open Close
2022-01-01 100 90
2022-01-02 101 78
2022-01-03 92 77
2022-01-04 103 98,
'tdf': Open Close
2022-01-01 80 70
2022-01-02 65 88
2022-01-03 42 79
2022-01-04 76 54,
'ghi': Open Close
2022-01-01 36 45
2022-01-02 55 78
2022-01-03 43 32
2022-01-04 45 65}

Plotting XRD pattern in Gnuplot

I wish to plot a XRD pattern using Gnuplot. The data file is given below
# User :
# Journal :
# Sample Details :
# Col 1: 2theta (deg.), Col 2: Intensity (a.u.)
20 88
20.05 92
20.1 96
20.15 88
20.2 84
20.25 100
20.3 94
20.35 84
20.4 78
20.45 81
20.5 86
20.55 92
20.6 85
20.65 74
20.7 83
20.75 74
20.8 87
20.85 67
20.9 85
20.95 83
21 90
21.05 81
21.1 87
21.15 84
21.2 83
21.25 84
21.3 86
21.35 92
21.4 92
21.45 73
21.5 97
21.55 96
21.6 88
21.65 95
21.7 93
21.75 83
21.8 91
21.85 91
21.9 76
21.95 85
22 89
22.05 87
22.1 72
22.15 85
22.2 71
22.25 68
22.3 75
22.35 63
22.4 64
22.45 76
22.5 72
22.55 78
22.6 62
22.65 80
22.7 80
22.75 78
22.8 85
22.85 62
22.9 74
22.95 84
23 73
23.05 76
23.1 83
23.15 48
23.2 74
23.25 79
23.3 75
23.35 70
23.4 94
23.45 80
23.5 79
23.55 69
23.6 73
23.65 55
23.7 72
23.75 59
23.8 87
23.85 76
23.9 70
23.95 76
24 74
24.05 76
24.1 89
24.15 79
24.2 84
24.25 72
24.3 69
24.35 74
24.4 72
24.45 85
24.5 65
24.55 85
24.6 68
24.65 71
24.7 70
24.75 70
24.8 75
24.85 73
24.9 79
24.95 76
25 78
25.05 68
25.1 67
25.15 62
25.2 72
25.25 65
25.3 78
25.35 74
25.4 67
25.45 77
25.5 70
25.55 82
25.6 80
25.65 64
25.7 71
25.75 67
25.8 70
25.85 73
25.9 80
25.95 69
26 61
26.05 82
26.1 86
26.15 83
26.2 88
26.25 68
26.3 78
26.35 66
26.4 71
26.45 69
26.5 61
26.55 73
26.6 74
26.65 83
26.7 72
26.75 82
26.8 84
26.85 72
26.9 68
26.95 72
27 63
27.05 66
27.1 68
27.15 65
27.2 75
27.25 72
27.3 66
27.35 58
27.4 67
27.45 73
27.5 74
27.55 84
27.6 80
27.65 82
27.7 70
27.75 71
27.8 70
27.85 76
27.9 73
27.95 85
28 82
28.05 75
28.1 70
28.15 85
28.2 80
28.25 96
28.3 126
28.35 173
28.4 219
28.45 270
28.5 214
28.55 158
28.6 105
28.65 78
28.7 75
28.75 82
28.8 83
28.85 74
28.9 79
28.95 77
29 61
29.05 75
29.1 75
29.15 65
29.2 69
29.25 79
29.3 68
29.35 78
29.4 74
29.45 57
29.5 75
29.55 65
29.6 69
29.65 83
29.7 72
29.75 69
29.8 65
29.85 68
29.9 89
29.95 79
30 84
30.05 110
30.1 129
30.15 146
30.2 150
30.25 136
30.3 117
30.35 81
30.4 72
30.45 85
30.5 75
30.55 71
30.6 70
30.65 72
30.7 87
30.75 67
30.8 71
30.85 75
30.9 80
30.95 76
31 78
31.05 72
31.1 66
31.15 85
31.2 61
31.25 62
31.3 69
31.35 75
31.4 51
31.45 48
31.5 55
31.55 77
31.6 66
31.65 84
31.7 64
31.75 69
31.8 74
31.85 53
31.9 77
31.95 68
32 74
32.05 53
32.1 67
32.15 61
32.2 68
32.25 77
32.3 62
32.35 57
32.4 62
32.45 62
32.5 62
32.55 66
32.6 56
32.65 55
32.7 62
32.75 61
32.8 67
32.85 74
32.9 49
32.95 66
33 82
33.05 57
33.1 59
33.15 61
33.2 62
33.25 74
33.3 60
33.35 72
33.4 66
33.45 71
33.5 58
33.55 66
33.6 59
33.65 66
33.7 53
33.75 70
33.8 52
33.85 66
33.9 58
33.95 65
34 47
34.05 51
34.1 56
34.15 55
34.2 77
34.25 70
34.3 64
34.35 59
34.4 62
34.45 51
34.5 62
34.55 48
34.6 45
34.65 59
34.7 65
34.75 64
34.8 83
34.85 75
34.9 125
34.95 149
35 152
35.05 120
35.1 84
35.15 76
35.2 70
35.25 63
35.3 68
35.35 51
35.4 64
35.45 62
35.5 58
35.55 63
35.6 67
35.65 68
35.7 62
35.75 58
35.8 59
35.85 48
35.9 53
35.95 55
36 54
36.05 56
36.1 49
36.15 53
36.2 66
36.25 60
36.3 63
36.35 61
36.4 48
36.45 67
36.5 61
36.55 50
36.6 48
36.65 46
36.7 70
36.75 54
36.8 62
36.85 62
36.9 45
36.95 56
37 44
37.05 59
37.1 58
37.15 53
37.2 49
37.25 49
37.3 54
37.35 51
37.4 48
37.45 49
37.5 56
37.55 47
37.6 53
37.65 50
37.7 47
37.75 48
37.8 64
37.85 61
37.9 56
37.95 44
38 44
38.05 43
38.1 45
38.15 51
38.2 61
38.25 48
38.3 39
38.35 58
38.4 50
38.45 56
38.5 49
38.55 54
38.6 47
38.65 47
38.7 46
38.75 49
38.8 43
38.85 50
38.9 51
38.95 60
39 69
39.05 46
39.1 68
39.15 44
39.2 42
39.25 46
39.3 52
39.35 43
39.4 46
39.45 52
39.5 57
39.55 46
39.6 48
39.65 47
39.7 51
39.75 61
39.8 48
39.85 53
39.9 59
39.95 46
40 53
40.05 51
40.1 49
40.15 56
40.2 49
40.25 50
40.3 52
40.35 58
40.4 62
40.45 99
40.5 119
40.55 161
40.6 135
40.65 163
40.7 130
40.75 105
40.8 80
40.85 68
40.9 53
40.95 55
41 53
41.05 54
41.1 59
41.15 56
41.2 53
41.25 52
41.3 46
41.35 47
41.4 44
41.45 57
41.5 49
41.55 48
41.6 42
41.65 45
41.7 46
41.75 48
41.8 44
41.85 51
41.9 45
41.95 54
42 44
42.05 46
42.1 43
42.15 53
42.2 50
42.25 38
42.3 42
42.35 51
42.4 51
42.45 42
42.5 36
42.55 36
42.6 53
42.65 44
42.7 38
42.75 45
42.8 55
42.85 33
42.9 50
42.95 58
43 48
43.05 46
43.1 51
43.15 44
43.2 58
43.25 47
43.3 47
43.35 59
43.4 42
43.45 50
43.5 50
43.55 45
43.6 48
43.65 51
43.7 38
43.75 51
43.8 57
43.85 49
43.9 59
43.95 40
44 41
44.05 39
44.1 37
44.15 43
44.2 43
44.25 52
44.3 50
44.35 46
44.4 41
44.45 48
44.5 44
44.55 40
44.6 47
44.65 39
44.7 49
44.75 49
44.8 44
44.85 38
44.9 44
44.95 53
45 46
45.05 51
45.1 31
45.15 38
45.2 51
45.25 42
45.3 47
45.35 46
45.4 37
45.45 66
45.5 53
45.55 40
45.6 45
45.65 39
45.7 47
45.75 39
45.8 45
45.85 38
45.9 48
45.95 42
46 35
46.05 46
46.1 45
46.15 34
46.2 55
46.25 39
46.3 35
46.35 50
46.4 32
46.45 49
46.5 50
46.55 36
46.6 46
46.65 51
46.7 41
46.75 41
46.8 53
46.85 54
46.9 50
46.95 43
47 44
47.05 61
47.1 36
47.15 43
47.2 48
47.25 38
47.3 46
47.35 48
47.4 51
47.45 42
47.5 51
47.55 37
47.6 40
47.65 49
47.7 33
47.75 36
47.8 41
47.85 49
47.9 39
47.95 42
48 58
48.05 51
48.1 43
48.15 48
48.2 46
48.25 34
48.3 44
48.35 39
48.4 44
48.45 46
48.5 35
48.55 35
48.6 51
48.65 43
48.7 45
48.75 54
48.8 48
48.85 41
48.9 49
48.95 52
49 36
49.05 42
49.1 38
49.15 44
49.2 44
49.25 38
49.3 49
49.35 54
49.4 52
49.45 39
49.5 41
49.55 66
49.6 51
49.65 38
49.7 34
49.75 52
49.8 46
49.85 60
49.9 47
49.95 45
50 68
50.05 66
50.1 68
50.15 89
50.2 133
50.25 137
50.3 125
50.35 124
50.4 103
50.45 88
50.5 72
50.55 59
50.6 56
50.65 46
50.7 39
50.75 51
50.8 27
50.85 45
50.9 41
50.95 43
51 51
51.05 49
51.1 45
51.15 38
51.2 48
51.25 43
51.3 42
51.35 44
51.4 45
51.45 49
51.5 40
51.55 43
51.6 37
51.65 50
51.7 47
51.75 37
51.8 39
51.85 43
51.9 42
51.95 40
52 45
52.05 34
52.1 32
52.15 43
52.2 37
52.25 47
52.3 62
52.35 36
52.4 45
52.45 36
52.5 37
52.55 38
52.6 37
52.65 35
52.7 38
52.75 47
52.8 40
52.85 50
52.9 50
52.95 44
53 44
53.05 35
53.1 46
53.15 43
53.2 40
53.25 57
53.3 29
53.35 39
53.4 39
53.45 43
53.5 51
53.55 44
53.6 53
53.65 40
53.7 41
53.75 59
53.8 46
53.85 46
53.9 57
53.95 52
54 48
54.05 50
54.1 40
54.15 42
54.2 46
54.25 48
54.3 47
54.35 42
54.4 49
54.45 43
54.5 43
54.55 54
54.6 44
54.65 40
54.7 45
54.75 34
54.8 42
54.85 40
54.9 34
54.95 43
55 51
55.05 56
55.1 42
55.15 37
55.2 41
55.25 49
55.3 53
55.35 46
55.4 47
55.45 39
55.5 43
55.55 43
55.6 41
55.65 38
55.7 36
55.75 55
55.8 45
55.85 37
55.9 47
55.95 50
56 46
56.05 51
56.1 51
56.15 56
56.2 31
56.25 44
56.3 41
56.35 45
56.4 45
56.45 49
56.5 45
56.55 60
56.6 30
56.65 38
56.7 51
56.75 54
56.8 45
56.85 38
56.9 40
56.95 41
57 36
57.05 35
57.1 45
57.15 41
57.2 49
57.25 36
57.3 38
57.35 43
57.4 43
57.45 41
57.5 42
57.55 42
57.6 41
57.65 39
57.7 37
57.75 48
57.8 42
57.85 56
57.9 45
57.95 39
58 51
58.05 40
58.1 47
58.15 39
58.2 46
58.25 37
58.3 41
58.35 36
58.4 50
58.45 42
58.5 58
58.55 36
58.6 59
58.65 57
58.7 57
58.75 68
58.8 61
58.85 68
58.9 58
58.95 54
59 53
59.05 59
59.1 48
59.15 49
59.2 42
59.25 49
59.3 45
59.35 38
59.4 44
59.45 54
59.5 37
59.55 52
59.6 62
59.65 70
59.7 73
59.75 88
59.8 69
59.85 62
59.9 60
59.95 51
60 59
60.05 51
60.1 45
60.15 45
60.2 39
60.25 55
60.3 48
60.35 45
60.4 34
60.45 47
60.5 32
60.55 40
60.6 40
60.65 54
60.7 35
60.75 50
60.8 40
60.85 34
60.9 39
60.95 31
61 27
61.05 45
61.1 47
61.15 41
61.2 34
61.25 46
61.3 42
61.35 33
61.4 45
61.45 41
61.5 31
61.55 42
61.6 40
61.65 32
61.7 35
61.75 33
61.8 45
61.85 48
61.9 50
61.95 44
62 36
62.05 34
62.1 44
62.15 47
62.2 40
62.25 42
62.3 50
62.35 51
62.4 43
62.45 46
62.5 59
62.55 50
62.6 56
62.65 63
62.7 67
62.75 54
62.8 61
62.85 54
62.9 43
62.95 44
63 36
63.05 49
63.1 41
63.15 46
63.2 44
63.25 36
63.3 34
63.35 36
63.4 58
63.45 42
63.5 40
63.55 48
63.6 43
63.65 35
63.7 36
63.75 42
63.8 41
63.85 40
63.9 39
63.95 40
64 32
64.05 46
64.1 44
64.15 41
64.2 44
64.25 35
64.3 45
64.35 46
64.4 37
64.45 43
64.5 36
64.55 40
64.6 36
64.65 45
64.7 38
64.75 42
64.8 37
64.85 39
64.9 50
64.95 40
65 28
65.05 34
65.1 41
65.15 42
65.2 40
65.25 45
65.3 34
65.35 44
65.4 49
65.45 49
65.5 46
65.55 36
65.6 40
65.65 51
65.7 30
65.75 48
65.8 34
65.85 52
65.9 32
65.95 43
66 41
66.05 42
66.1 61
66.15 57
66.2 55
66.25 44
66.3 63
66.35 78
66.4 80
66.45 77
66.5 98
66.55 86
66.6 81
66.65 61
66.7 62
66.75 59
66.8 54
66.85 50
66.9 52
66.95 32
67 35
67.05 39
67.1 47
67.15 39
67.2 51
67.25 53
67.3 35
67.35 41
67.4 38
67.45 25
67.5 48
67.55 41
67.6 37
67.65 45
67.7 37
67.75 29
67.8 32
67.85 28
67.9 31
67.95 41
68 33
68.05 44
68.1 48
68.15 39
68.2 46
68.25 49
68.3 39
68.35 40
68.4 50
68.45 40
68.5 54
68.55 44
68.6 35
68.65 31
68.7 46
68.75 28
68.8 38
68.85 29
68.9 34
68.95 43
69 49
69.05 37
69.1 43
69.15 44
69.2 31
69.25 33
69.3 43
69.35 47
69.4 42
69.45 39
69.5 37
69.55 35
69.6 42
69.65 44
69.7 37
69.75 53
69.8 24
69.85 35
69.9 41
69.95 34
70 42
70.05 37
70.1 22
70.15 47
70.2 35
70.25 31
70.3 52
70.35 37
70.4 41
70.45 40
70.5 47
70.55 44
70.6 39
70.65 47
70.7 30
70.75 39
70.8 41
70.85 36
70.9 25
70.95 41
71 33
71.05 35
71.1 32
71.15 35
71.2 38
71.25 45
71.3 45
71.35 57
71.4 41
71.45 30
71.5 28
71.55 34
71.6 42
71.65 41
71.7 32
71.75 25
71.8 43
71.85 45
71.9 33
71.95 34
72 44
72.05 43
72.1 45
72.15 42
72.2 42
72.25 30
72.3 44
72.35 47
72.4 36
72.45 45
72.5 40
72.55 45
72.6 33
72.65 39
72.7 47
72.75 39
72.8 40
72.85 34
72.9 39
72.95 30
73 31
73.05 37
73.1 35
73.15 39
73.2 37
73.25 34
73.3 38
73.35 41
73.4 34
73.45 50
73.5 46
73.55 44
73.6 54
73.65 57
73.7 81
73.75 71
73.8 83
73.85 80
73.9 70
73.95 50
74 61
74.05 39
74.1 58
74.15 46
74.2 33
74.25 40
74.3 41
74.35 38
74.4 48
74.45 49
74.5 46
74.55 39
74.6 46
74.65 43
74.7 31
74.75 37
74.8 32
74.85 32
74.9 44
74.95 34
75 29
75.05 31
75.1 33
75.15 52
75.2 32
75.25 29
75.3 29
75.35 36
75.4 28
75.45 28
75.5 45
75.55 25
75.6 30
75.65 26
75.7 32
75.75 32
75.8 44
75.85 40
75.9 30
75.95 34
76 44
76.05 41
76.1 46
76.15 30
76.2 39
76.25 38
76.3 43
76.35 30
76.4 47
76.45 31
76.5 34
76.55 36
76.6 45
76.65 36
76.7 32
76.75 30
76.8 34
76.85 37
76.9 37
76.95 41
77 37
77.05 30
77.1 40
77.15 47
77.2 34
77.25 44
77.3 35
77.35 41
77.4 33
77.45 36
77.5 29
77.55 38
77.6 32
77.65 45
77.7 34
77.75 29
77.8 36
77.85 33
77.9 35
77.95 34
78 40
78.05 43
78.1 33
78.15 35
78.2 40
78.25 43
78.3 38
78.35 38
78.4 43
78.45 40
78.5 42
78.55 38
78.6 34
78.65 34
78.7 40
78.75 37
78.8 37
78.85 39
78.9 47
78.95 40
79 35
79.05 34
79.1 40
79.15 39
79.2 33
79.25 38
79.3 32
79.35 48
79.4 39
79.45 35
79.5 37
79.55 32
79.6 40
79.65 36
79.7 40
79.75 40
79.8 26
79.85 39
79.9 49
79.95 36
80 43
My script is provided below
set terminal postscript eps enhanced colour font 'Times-Roman,12' size 4in,3in
set output "XRD_E1.eps"
set style line 1 lt 1 lw 1 lc rgb "black"
set xtics out scale 1.5
set ytics out scale 1.5
set yrange [0:350]
set format y ''
set tics font ",16"
set xlabel "2{/Symbol \161} (deg.)" font ",16"
set ylabel "Intensity (a.u.)" font ",16"
set key inside right top
set key title "As prepared LiCl-KCl \n " font ",14"
##
set label 1 '(200)' font ",12" at 28.45, 280 point pt 7 ps 1.75 lt rgb "red" offset -0.50, 0.50 rotate by 90
set label 2 '(111)' font ",12" at 30.20, 160 point pt 7 ps 1.75 lt rgb "blue" offset -0.50,0.50 rotate by 90
set label 3 '(200)' font ",12" at 35.00, 160 point pt 7 ps 1.75 lt rgb "blue" offset -0.50,0.50 rotate by 90
set label 4 '(220)' font ",12" at 40.65, 173 point pt 7 ps 1.75 lt rgb "red" offset -0.50,0.50 rotate by 90
set label 5 '(220)' font ",12" at 50.25, 147 point pt 7 ps 1.75 lt rgb "blue" offset -0.50,0.50 rotate by 90
set label 6 '(400)' font ",12" at 58.85, 78 point pt 7 ps 1.75 lt rgb "red" offset -1.00,0.50 rotate by 90
set label 7 '(311)' font ",12" at 59.75, 98 point pt 7 ps 1.75 lt rgb "blue" offset -0.50,0.50 rotate by 90
set label 8 '(222)' font ",12" at 62.70, 77 point pt 7 ps 1.75 lt rgb "blue" offset -0.50,0.50 rotate by 90
set label 9 '(420)' font ",12" at 66.50, 108 point pt 7 ps 1.75 lt rgb "red" offset -0.50,0.50 rotate by 90
set label 10 '(222)' font ",12" at 73.80, 93 point pt 7 ps 1.75 lt rgb "blue" offset -0.50,0.50 rotate by 90
##
plot [10:90] 'XRD_E1.dat' u 1:2 w l ls 1 notitle
The script works fine. I only need to include the point types used in the plot in the key title with a proper spacing.
Secondly, is there a better way to plot and label than what is provided in the script?
Thank you
#Suddhasattwa Ghosh, in case this is still of interest, you can simply plot your marks with labels point (check help labels). Put your peak data into a datablock or in separate files.
Script:
### print with labels and points
reset session
FILE = 'XRD_E1.dat'
set tics font ",16"
set xlabel "2{/Symbol \161} (deg.)" font ",16"
set xtics out scale 1.5
set ylabel "Intensity (a.u.)" font ",16"
set yrange [0:350]
set format y ''
set ytics out scale 1.5
set key inside right top noautotitle font ",12" title "As prepared LiCl-KCl \n " font ",14"
$LiCl <<EOD
(200) 28.45 280
(220) 40.65 173
(400) 58.85 78
(420) 66.50 108
EOD
$KCl <<EOD
(111) 30.20 160
(200) 35.00 160
(220) 50.25 147
(311) 59.75 98
(222) 62.70 77
(222) 73.80 93
EOD
plot FILE u 1:2 w l lc "black", \
$LiCl u 2:3:1 w labels point pt 7 lc "red" offset -0.5,0.5 left rotate by 90 font ",12" ti "LiCl", \
$KCl u 2:3:1 w labels point pt 7 lc "blue" offset -0.5,0.5 left rotate by 90 font ",12" ti "KCl"
### end of script
Result:
If I understand well, you want something like this:
To achieve this, extend your code with:
set key horizontal
plot [10:90] 'XRD_E1.dat' u 1:2 w l ls 1 notitle, 1/0 w p pt 7 ps 1.75 lc rgb "red" t 'LiCl', 1/0 w p pt 7 ps 1.75 lc rgb "blue" t 'KCl'

How is this a series in Pandas?

The below code produces a Pandas series:
import pandas as pd
df = pd.read_csv(path)
s = df.groupby(['Pregnancies', 'Glucose'])['BloodPressure'].sum()
print(s)
I know I can make it a dataframe by using reset_index(). But I am confused how the below is a series seeing as a series should be a 1D array?
Pregnancies Glucose
0 57 60
67 76
73 0
74 52
78 88
84 146
86 68
91 148
93 220
94 70
95 229

Issue with loading text file using numpy loadtxt

I am trying to load a text file which contains a set of arrays which looks like:
[ 90 91 92 93 94 95 96 97 100 101 102 103 157 158 159 160]
[ 58 59 60 61 62 63 76 77 78 79 80 81 82 83 84 85 86 87
88 89 90 91 92 93 94 95 96 97 98 99 100 102 103 104 105 108
109 110 111 127 128 129 130 131 132 133 134 135 137 138 139 140 145 146
147 148 171 172 173]
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
18 23 24 25 26 27 47 48 49 50 51 52 53 54 55 56 57 58
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
77 78 79 80 81 82 83 84 85 86 87 88 89 165 166 167 168 169
170 171 172 173]
I have tried using test = np.loadtxt('filename.txt') to load the text file but I keep getting this issue: could not convert string to float: [. The file does not contain any headers. Any help would be much appreciated. Thank you.
The following code will return a list of lists containing integers. This assumes that the data you have is all integers. If not, you can change the map call to convert to float instead:
import os.path as path
# Path to file.
path_to_data = path.join(path.dirname(path.realpath(__file__)),
'data.txt')
print(path_to_data)
# Read file contents.
with open(path_to_data) as file:
data = file.read()
print(data)
# Now we need to parse it for square braces.
data = data.replace("\n", " ").replace(" ", " ").replace(" ", " ")
data = data.replace("[ ", "[").replace(" ]", "]")
print(data)
# Now we loop through the string looking for [ symbols.
data_list = []
start_index = 0
end_index = 0
while 0 <= data.find('[', start_index):
start_index = data.find('[', start_index)
if 0 <= start_index <= data.find(']', start_index):
end_index = data.find(']', start_index)
# Don't include the braces, actually: grab what's in-between.
temp_str = data[start_index+1:end_index]
# Now split into a list based on spaces.
data_list.append(list(map(int, list(temp_str.split(" ")))))
start_index = end_index
print(data_list)

Resources