使用数据框的每两列创建一个新的数据框

时间:2020-03-05 19:22:48

标签: python pandas

我是Python的新手,为此我感到困惑。

我想创建许多新数据框,每个新数据框都来自现有数据框的列。原始格式为Time,x1,Time2,x2 ...

我已经找到了搜索“时间”的循环

for col in df.columns:
    if 'Time' in col:

我需要调用找到的列及其旁边的列,并将其分配给具有['Time','x1']列的新数据框,然后为每对Timen&xn循环遍历。我想用xn命名数据帧。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

如果我正确解释了您的问题,我认为您想要的是import * as FBCommon from '@react-native-firebase/app/lib/common' jest.mock(FBCommon, () => { return () => ({ isBoolean: jest.fn(), isNumber: jest.fn(), isString: jest.fn() }) });

如果您的数据框始终设置为jest.mock('@react-native-firebase/database', () => { return () => ({ ref: jest.fn() }) }); 格式(例如,您一直在抓取连续的列以创建新的数据框),则可以使用以下操作:

{
  "name": "Project",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint ."
  },
  "dependencies": {
    "@react-native-community/push-notification-ios": "^1.0.6",
    "@react-native-firebase/app": "^6.3.1",
    "@react-native-firebase/database": "^6.3.1",
    "@react-native-firebase/messaging": "^6.3.1",
    "axios": "^0.19.0",
    "base-64": "^0.1.0",
    "blinkid-react-native": "^5.2.0",
    "firebase": "^7.6.0",
    "lodash": "^4.17.15",
    "mobx": "^5.15.0",
    "mobx-persist": "^0.4.1",
    "mobx-react": "^6.1.4",
    "react": "16.9.0",
    "react-native": "0.61.4",
    "react-native-auth0": "^2.1.0",
    "react-native-collapsible": "^1.5.1",
    "react-native-config": "^0.12.0",
    "react-native-confirmation-code-input": "^1.0.4",
    "react-native-datepicker": "^1.7.2",
    "react-native-device-info": "^5.4.1",
    "react-native-elements": "^1.2.7",
    "react-native-freshchat-sdk": "^2.3.0",
    "react-native-gesture-handler": "^1.5.2",
    "react-native-in-app-notification": "^3.0.1",
    "react-native-keyboard-aware-scroll-view": "^0.9.1",
    "react-native-phone-input": "^0.2.4",
    "react-native-picker-select": "^6.3.3",
    "react-native-push-notification": "^3.1.9",
    "react-native-reanimated": "^1.4.0",
    "react-native-screens": "^1.0.0-alpha.23",
    "react-native-signature-capture": "^0.4.10",
    "react-native-vector-icons": "^6.6.0",
    "react-native-webview": "^8.0.3",
    "react-navigation": "^4.0.10",
    "react-navigation-stack": "^1.10.3"
  },
  "devDependencies": {
    "@babel/core": "^7.7.2",
    "@babel/runtime": "^7.7.2",
    "@react-native-community/eslint-config": "^0.0.5",
    "babel-jest": "^24.9.0",
    "babel-plugin-module-resolver": "^4.0.0",
    "chai": "^4.1.2",
    "chai-enzyme": "1.0.0-beta.0",
    "enzyme": "^3.3.0",
    "enzyme-adapter-react-16": "^1.1.1",
    "eslint": "^6.6.0",
    "jest": "^24.9.0",
    "jsdom": "15.2.1",
    "jsdom-global": "3.0.2",
    "metro-react-native-babel-preset": "^0.57.0",
    "react-dom": "^16.12.0",
    "react-test-renderer": "16.9.0",
    "sinon": "^7.2.2"
  },
  "jest": {
    "preset": "react-native",
    "setupFilesAfterEnv": [
      "./setUpTests.js"
    ],
    "transformIgnorePatterns": [
      "node_modules/(?!((jest-)?react-native|@react-native-firebase/database|@react-native-firebase/app|react-clone-referenced-element|expo(nent)?|@expo(nent)?/.*|react-navigation|@react-navigation/.*|sentry-expo|native-base))"
    ]
  }
}

df.iloc[<row index>, <column index>]中的Time1, x1, Time2, x2, ..., TimeN, xN将选择所有行,而df_1 = df.iloc[ : , [0,1] ] 中的:是所需的列索引列表。

然后您可以遍历原始数据框中的列数以获取每一对:

<row_index>