重建索引为['赵一','钱二','孙三','李四','周五','钱多多']来创建新的数据框df1,补充钱多多的score1和address为60、厦门
时间: 2023-05-21 09:03:54 浏览: 41
可以使用以下代码来重建索引并创建新的数据框df1:
```
import pandas as pd
data = {'name': ['赵一', '钱二', '孙三', '李四', '周五', '钱多多'],
'score': [80, 90, 85, 70, 95, None],
'address': ['北京', '上海', '广州', '深圳', '重庆', None]}
df = pd.DataFrame(data)
df1 = df.set_index('name').reindex(['赵一', '钱二', '孙三', '李四', '周五', '钱多多']).reset_index()
df1.loc[df1['name'] == '钱多多', ['score', 'address']] = [60, '厦门']
```
这段代码将原始数据框df按照name列进行索引,然后使用reindex方法重建索引为['赵一','钱二','孙三','李四','周五','钱多多'],最后使用reset_index方法将name列还原为普通列。然后,使用loc方法找到name为'钱多多'的行,并将其score和address修改为60和'厦门'。
相关问题
name = ['赵一','钱二','孙三','李四','周五','吴六','郑七'] s1 = [80,99,98,65,32,33,85] s2 = [77,85,95,62,0,53,25] address = ['兰州','成都','贵阳','拉萨','长春','沈阳','上海'] 将score1、score2、address作为值,name作为索引,['score1','score2','address']为列名,创建数据框df,展示df的前五行
可以使用 pandas 库来创建数据框,代码如下:
```python
import pandas as pd
name = ['赵一','钱二','孙三','李四','周五','吴六','郑七']
s1 = [80,99,98,65,32,33,85]
s2 = [77,85,95,62,0,53,25]
address = ['兰州','成都','贵阳','拉萨','长春','沈阳','上海']
df = pd.DataFrame({'score1': s1, 'score2': s2, 'address': address}, index=name)
print(df.head())
```
输出结果如下:
```
score1 score2 address
赵一 80 77 兰州
钱二 99 85 成都
孙三 98 95 贵阳
李四 65 62 拉萨
周五 32 0 长春
```
用Java编写实验内容:赵一、钱二、孙三三位同学来自A省,该三位同学进入大学之前英语成绩采用百分制;李四、王五、张六三位同学来自B省,该三位同学进入大学之前英语成绩采用五分制。六位同学的成绩如下: 学号 姓名 成绩 10001 赵一 85 10002 钱二 74 10003 孙三 68 10004. 李四 B 10005 王五 A 10006 张六 C_ 编程要求1:通过泛型类设 置set、get和打印等方法,将以上六位同学的成绩在控制台打印出来; 编程要求2:将以上数据先写入到数据库StudengScore表中进行记录,便于今后查询(注:先通过eclipse写入,之后从数据库里读出)。,
以下是用Java编写的实验内容:
1. 定义学生类:
```
public class Student<T> {
private String id;
private String name;
private T score;
public Student(String id, String name, T score) {
this.id = id;
this.name = name;
this.score = score;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public T getScore() {
return score;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setScore(T score) {
this.score = score;
}
@Override
public String toString() {
return "学号:" + id + ",姓名:" + name + ",成绩:" + score;
}
}
```
2. 定义主函数,创建学生对象并打印:
```
public class Main {
public static void main(String[] args) {
// 创建学生对象
Student<Integer> s1 = new Student<>("10001", "赵一", 85);
Student<Integer> s2 = new Student<>("10002", "钱二", 74);
Student<Integer> s3 = new Student<>("10003", "孙三", 68);
Student<String> s4 = new Student<>("10004", "李四", "B");
Student<String> s5 = new Student<>("10005", "王五", "A");
Student<String> s6 = new Student<>("10006", "张六", "C-");
// 打印学生信息
System.out.println(s1.toString());
System.out.println(s2.toString());
System.out.println(s3.toString());
System.out.println(s4.toString());
System.out.println(s5.toString());
System.out.println(s6.toString());
}
}
```
3. 将学生信息写入数据库:
```
import java.sql.*;
public class Main {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 加载数据库驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 获取数据库连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?serverTimezone=UTC", "root", "123456");
// 创建PreparedStatement对象
String sql = "INSERT INTO StudentScore(id, name, score) VALUES (?, ?, ?)";
pstmt = conn.prepareStatement(sql);
// 设置参数并执行
Student<Integer> s1 = new Student<>("10001", "赵一", 85);
pstmt.setString(1, s1.getId());
pstmt.setString(2, s1.getName());
pstmt.setInt(3, s1.getScore());
pstmt.executeUpdate();
Student<Integer> s2 = new Student<>("10002", "钱二", 74);
pstmt.setString(1, s2.getId());
pstmt.setString(2, s2.getName());
pstmt.setInt(3, s2.getScore());
pstmt.executeUpdate();
Student<Integer> s3 = new Student<>("10003", "孙三", 68);
pstmt.setString(1, s3.getId());
pstmt.setString(2, s3.getName());
pstmt.setInt(3, s3.getScore());
pstmt.executeUpdate();
Student<String> s4 = new Student<>("10004", "李四", "B");
pstmt.setString(1, s4.getId());
pstmt.setString(2, s4.getName());
pstmt.setString(3, s4.getScore());
pstmt.executeUpdate();
Student<String> s5 = new Student<>("10005", "王五", "A");
pstmt.setString(1, s5.getId());
pstmt.setString(2, s5.getName());
pstmt.setString(3, s5.getScore());
pstmt.executeUpdate();
Student<String> s6 = new Student<>("10006", "张六", "C-");
pstmt.setString(1, s6.getId());
pstmt.setString(2, s6.getName());
pstmt.setString(3, s6.getScore());
pstmt.executeUpdate();
System.out.println("数据已成功写入数据库!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
4. 从数据库中读取学生信息并打印:
```
import java.sql.*;
public class Main {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 加载数据库驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 获取数据库连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?serverTimezone=UTC", "root", "123456");
// 创建Statement对象
stmt = conn.createStatement();
// 执行查询并获取结果集
String sql = "SELECT * FROM StudentScore";
rs = stmt.executeQuery(sql);
// 打印学生信息
while (rs.next()) {
String id = rs.getString("id");
String name = rs.getString("name");
String score = rs.getString("score");
System.out.println("学号:" + id + ",姓名:" + name + ",成绩:" + score);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
阅读全文