我有一个这样的数据框:
id date company ......
123 2019-01-01 A
224 2019-01-01 B
345 2019-01-01 B
987 2019-01-03 C
334 2019-01-03 C
908 2019-01-04 C
765 2019-01-04 A
554 2019-01-05 A
482 2019-01-05 D
Run Code Online (Sandbox Code Playgroud)
并且我想获取“公司”列随时间的唯一值的累积数量。因此,如果公司在以后出现,则不会再计算在内。
我的预期输出是:
date cumulative_count
2019-01-01 2
2019-01-03 3
2019-01-04 3
2019-01-05 4
Run Code Online (Sandbox Code Playgroud)
我试过了:
df.groupby(['date']).company.nunique().cumsum()
Run Code Online (Sandbox Code Playgroud)
但是,如果同一家公司在不同的日期出现,则此重复计算。
我有两个这样的数据框:
df1
sales day ...
index
1001 567 321
1002 600 1530
1005 789 998
1008 825 775
1002 300 1120
Run Code Online (Sandbox Code Playgroud)
df2
sales day ...
index
1001 567 321
1002 600 1530
1005 789 998
1014 620 1000
1008 825 775
1009 589 1100
1002 300 1120
1005 770 400
1008 820 1600
Run Code Online (Sandbox Code Playgroud)
我只想保留 df2 中索引和日期值与 df1 完全相同的行
所以结果应该是:
df2
sales day ...
index
1001 567 321
1002 600 1530
1005 789 998
1008 825 775
1002 300 1120
Run Code Online (Sandbox Code Playgroud)
我无法使用 …
我有 2 个 numpy 数组,如下所示:
a = np.array([32.0, 25.97, 26.78, 35.85, 30.17, 29.87, 30.45, 31.93, 30.65, 35.49,
28.3, 35.24, 35.98, 38.84, 27.97, 26.98, 25.98, 34.53, 40.39, 36.3])
b = np.array([28.778585, 31.164268, 24.690865, 33.523693, 29.272448, 28.39742,
28.950092, 29.701189, 29.179174, 30.94298 , 26.05434 , 31.793175,
30.382706, 32.135723, 28.018875, 25.659306, 27.232124, 28.295502,
33.081223, 30.312504])
Run Code Online (Sandbox Code Playgroud)
当我使用 SciKit Learn 计算 R 平方时,我得到的值与计算 Pearson 相关性然后对结果求平方时完全不同的值:
sk_r2 = sklearn.metrics.r2_score(a, b)
print('SciKit R2: {:0.5f}\n'.format(sk_r2))
pearson_r = scipy.stats.pearsonr(a, b)
print('Pearson R: ', pearson_r)
print('Pearson R squared: ', pearson_r[0]**2) …Run Code Online (Sandbox Code Playgroud) python scikit-learn pearson-correlation scipy.stats coefficient-of-determination
python ×3
dataframe ×2
pandas ×2
coefficient-of-determination ×1
datetime ×1
filter ×1
scikit-learn ×1
scipy.stats ×1