从熊猫添加数据到列表

时间:2019-02-18 18:13:02

标签: python-3.x pandas list

Python初学者在这里,我想将数据框中的值提取到列表中,但是我得到了不需要的额外信息。有没有更好的方法可以做到这一点:

   <!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<script src="js/howler.core.js"></script>
<link rel="stylesheet" href="css/styles.css">

</head>

<body>

<script>

    setInterval(function () {
        masterClock();
    }, 2000);

    function masterClock() {

        var potentialThunderSounds = [
            getSoundB(),
            getSoundB(),
            getSoundB()
        ];

        playThunder(1);

        function playThunder(numberOfSoundsToPlay) {
            var soundSequence = [];
            for (var x = 0; x < numberOfSoundsToPlay; x++) {
                var soundIndex = 1;
                soundSequence.push(potentialThunderSounds[soundIndex]);
            }

            playSoundIfThereIsOne();
            
            function playSoundIfThereIsOne() {
                var currentSound = soundSequence[0];
                currentSound.play();
                soundSequence.shift();
                currentSound.once('end', playSoundIfThereIsOne);
            }
        }

        function getSoundB() {
            return new Howl({
                src: ['audio/1.mp3'],
                autoplay: false,
                loop: false,
                volume: 1,
                fade: 0
            });
        }
    }

</script>

<script src="js/howler.core.js"></script>
<script src="js/siriwave.js"></script>
<script src="js/player.js"></script>

</body>

</html>

当我打印等级1时,我得到了:

rating1 = []
rating2 = []
for value in person1["Movie"]:
    for value2 in person2["Movie"]:
        if value == value2:
            rating1.append(person1[person1["Movie"] == value]["Rating"])
            rating2.append(person2[person2["Movie"] == value2]["Rating"])

我的目标是仅提取不包含索引和其他信息的评分,以用于计算曼哈顿和欧几里得距离。 像这样:

print(rating1)
[0    2.5
Name: Rating, dtype: float64, 1    3.5
Name: Rating, dtype: float64, 2    2.5
Name: Rating, dtype: float64, 5    3.0
Name: Rating, dtype: float64, 22    3.5
Name: Rating, dtype: float64, 23    3.0
Name: Rating, dtype: float64]

1 个答案:

答案 0 :(得分:0)

我已经找到问题的答案,在这里供将来参考。 使用了append方法,将其更改为扩展方法,结果正是我想要的。

rating1 = []
rating2 = []
for value in person1["Movie"]:
    for value2 in person2["Movie"]:
        if value == value2:
            rating1.extend(person1[person1["Movie"] == value]["Rating"])
            rating2.extend(person2[person2["Movie"] == value2]["Rating"])

print(rating1)
>>>[2.5, 3.5, 2.5, 3.0, 3.5, 3.0]

这样,我可以像这样调用Euclidean和Manhattan方法:

from scipy.spatial import distance
r1 = np.array(rating1)
r2 = np.array(rating2)

euclidean = distance.euclidean(r1, r2)
manhattan = distance.cityblock(r1, r2)