Python 颠倒一系列清单
我有以下名为“水果”的熊猫系列:Python 颠倒一系列清单,python,pandas,series,Python,Pandas,Series,我有以下名为“水果”的熊猫系列: Out[33]: Apples 0 [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, ... 1 [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, ... 2 [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, ... 3 [0.0, 1.0, 2.0, 3.0, 4
Out[33]:
Apples
0 [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, ...
1 [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, ...
2 [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, ...
3 [0.0, 1.0, 2.0, 3.0, 4.0]
4 [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, ...
5 [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, ...
6 [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, ...
7 [0.0, 1.0, 2.0, 3.0, 4.0]
8 [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, ...
9 [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]
dtype: object
我想反转每一行(水平)。我使用的是代码Fruits[::-1],但输出与索引Apples(列)相反。有什么办法可以把一系列的故事颠倒过来?像这样吗
public void foobar() {
int[][] data = new int[][] {
{ 1, 2, 3, 4, 5 },
{ 1, 2, 3, 4, 5 },
{ 1, 2, 3, 4, 5 },
{ 1, 2, 3, 4, 5 },
{ 1, 2, 3, 4, 5 }
};
loop(data);
System.out.println("=>");
loopReverse(data);
}
public void loop(int[][] data) {
for(int i=0; i<data.length; i++) {
System.out.print(i+"\t");
for(int j=0; j<data[i].length; j++) {
System.out.print("["+data[i][j] + "] ");
}
System.out.println();
}
}
public void loopReverse(int[][] data) {
for(int i=data.length-1; i >= 0; i--) {
System.out.print(i+"\t");
for(int j=data[i].length-1; j >= 0; j--) {
System.out.print("["+data[i][j] + "] ");
}
System.out.println();
}
}
像这样
public void foobar() {
int[][] data = new int[][] {
{ 1, 2, 3, 4, 5 },
{ 1, 2, 3, 4, 5 },
{ 1, 2, 3, 4, 5 },
{ 1, 2, 3, 4, 5 },
{ 1, 2, 3, 4, 5 }
};
loop(data);
System.out.println("=>");
loopReverse(data);
}
public void loop(int[][] data) {
for(int i=0; i<data.length; i++) {
System.out.print(i+"\t");
for(int j=0; j<data[i].length; j++) {
System.out.print("["+data[i][j] + "] ");
}
System.out.println();
}
}
public void loopReverse(int[][] data) {
for(int i=data.length-1; i >= 0; i--) {
System.out.print(i+"\t");
for(int j=data[i].length-1; j >= 0; j--) {
System.out.print("["+data[i][j] + "] ");
}
System.out.println();
}
}
似乎您需要
str[:-1]
:
Fruits = pd.Series(
[[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2],
[0, 1, 2, 3, 4],
[0, 1]]).rename_axis('Apples')
print (Fruits)
Apples
0 [0, 1, 2, 3, 4]
1 [0, 1, 2, 3, 4]
2 [0, 1, 2]
3 [0, 1, 2, 3, 4]
4 [0, 1]
dtype: object
print(Fruits.str[::-1])
Apples
0 [4, 3, 2, 1, 0]
1 [4, 3, 2, 1, 0]
2 [2, 1, 0]
3 [4, 3, 2, 1, 0]
4 [1, 0]
dtype: object
似乎您需要
str[:-1]
:
Fruits = pd.Series(
[[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2],
[0, 1, 2, 3, 4],
[0, 1]]).rename_axis('Apples')
print (Fruits)
Apples
0 [0, 1, 2, 3, 4]
1 [0, 1, 2, 3, 4]
2 [0, 1, 2]
3 [0, 1, 2, 3, 4]
4 [0, 1]
dtype: object
print(Fruits.str[::-1])
Apples
0 [4, 3, 2, 1, 0]
1 [4, 3, 2, 1, 0]
2 [2, 1, 0]
3 [4, 3, 2, 1, 0]
4 [1, 0]
dtype: object
你试过使用for循环吗?你试过使用for循环吗?这几乎就是我想要的。不管我用的是python,你用的是java吗?这几乎就是我想要的。不过我用的是python,你用的是java吗?很高兴能帮上忙,祝你愉快!很高兴能帮上忙,天气真好!